Doctrine Many to Many Insert

2020-03-21 09:37发布

问题:

i have some promblem. i was research and try all suggest but no one work.

and i end up with :

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

i have manytomany relationship, user, user_category, and category

user

<?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;
    }

}

category

<?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;
    }
}

Insert function

        // 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();

Everything goes fine on get but i'm so confuse for set to work.

thanks for your help. sorry for my english.

回答1:

You have lot of errors (pay attention to grammar):

instead of

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

it should be:

protected $categories;

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

instead of:

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

use

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

Instead of

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

it must be

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

and

public function removeCategory(Category $category)
{
    $this->categories->removeElement($category) ;
}
public function setCategories($categories)
{
    $this->categories = categories;
}

Same logic on both sides. I don't know how CI works but Symfony will automatically find addSomething/removeSomething methods. Even if CI doesn't support that feature, you should still change your code as above.