Doctrine多对多插入

6

我有一些问题。我已经研究并尝试了所有的建议,但都无法解决。

最终我得到了以下错误:

实体\User::addCategories() 的第一个参数必须是 Entity\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();

在获取方面一切都很顺利,但我对设置工作感到困惑。

谢谢你的帮助。 抱歉我的英语不好。


addCategory方法需要一个Category对象,而你却传递了一个字符串。虽然你没有发布代码的那部分,但错误信息已经说明了问题。 - DevZer0
那么更新这3个表的正确方法是什么?或者我必须传递哪种类型到addCategory?我已经尝试了各种方法,但仍然遇到错误。 - dhidy
我更新了我的帖子,您能再次看一下吗? - dhidy
我不明白为什么你把一个类别对象设置到一个叫做categories的变量中。那里有些不对劲。 - DevZer0
你能告诉我你期望哪一行出错了吗?我已经尝试了,但仍然出现了相同的错误,但是这个错误带有boolean given,并且在D:\WEBSERVER\xampp\htdocs\codetrine\application\models\Entity\category.php on line 66中出现了“在非对象上调用成员函数addUser()”。 - dhidy
1个回答

10
你有很多错误(注意语法):
而不是:
public $categories;
public function __construct() {
    $this->category = new \Doctrine\Common\Collections\ArrayCollection();
}

应该是:

protected $categories;

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

改为:

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

使用

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

与其

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

必须是这样

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

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

双方都使用相同的逻辑。我不知道CI如何工作,但Symfony将自动查找addSomething/removeSomething方法。即使CI不支持该功能,您仍应按上述方式更改代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接