6

私はいくつかの問題を抱えています。私は研究していて、すべての提案を試みましたが、誰もうまくいきませんでした。

そして私は次のようになります:

Argument 1 passed to Entity\User::addCategories() must be an instance of Entity\Category, string given,

私は多対多の関係、ユーザー、user_category、およびカテゴリを持っています

ユーザー

<?php

namespace Entity;

use Doctrine\Common\Collections\ArrayCollection;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * @Entity
 * @Table(name="user")
 */
class User
{

    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @Column(type="string", length=255, unique=true, nullable=false)
     */
    public $name;


    /**
     * @ManyToMany(targetEntity="Entity\Category", inversedBy="user")
     * @ORM\JoinTable(name="user_category")
     */
    public $categories;

        public function __construct() {
            $this->category = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getCategories()
    {
        return $this->categories;
    }

    public function addCategories(Category $category = null)
    {
        $this->categories = $category;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
    public function getName()
    {
        return $this->name;
    }

}

カテゴリー

<?php

namespace Entity;

use Doctrine\Common\Collections\ArrayCollection;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * @Entity
 * @Table(name="category")
 */
class Category
{

    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @Column(type="string", length=255, unique=true, nullable=false)
     */
    public $name;

    /**
     * @ManyToMany(targetEntity="Entity\User", mappedBy="category")
     */
    public $user;


    public function __construct() {
        $this->user = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getUser()
    {
        return $this->user;
    }

    public function addUser(User $user = null)
    {
        $user->addCategory($this);
        $this->user = $user;
    }


    public function setName($name)
    {
        $this->name = $name;
    }
    public function getName()
    {
    return $this->name;
    }
}

挿入機能

        // check existence object in database
        $res = $this->em->find('Entity\User', $this->input->post('id'));

        if($res){
            $data = $this->em->find('Entity\User', $this->input->post('id'));
        }else{
            // create a new User object
            $data = new Entity\User;                
        }


        $data->setName($this->input->post('name'));
        $data->addCategories($this->input->post('category'));


        // save the data object to the database
        $this->em->persist($data);

        $this->em->flush();

get ではすべてがうまくいきますが、set が機能するのはとても混乱しています。

ご協力いただきありがとうございます。私の英語でごめんなさい。

4

1 に答える 1