许多插入的教义 [英] Doctrine Many to Many Insert

查看:181
本文介绍了许多插入的教义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些征兆。我是研究,尝试所有建议,但没有一个工作。

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

和我结束:


传递给Entity \User :: addCategories()的参数1必须是Entity \Category的一个实例,string given,

我有manytomany关系,用户,user_category和类别

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

插入函数 p>

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.

感谢您的帮助。

推荐答案

您有很多错误(注意语法):

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

而不是

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不支持该功能,您仍然应该如上更改您的代码。

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.

这篇关于许多插入的教义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆