Symfony2-Doctrine:ManyToMany关系不保存到数据库 [英] Symfony2-Doctrine: ManyToMany relation is not saved to database

查看:73
本文介绍了Symfony2-Doctrine:ManyToMany关系不保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为Category和Item的PHP模型类。类别可能有许多项目,项目可能属于许多类别。
我已经创建了一个与两个类的ManyToMany关系:

  class Category 
{
/ **
* @ ORM\ManyToMany(targetEntity =Item,mappedBy =categories,cascade = {persist})
* /
private $ items;

/ **
*添加项
*
* @param Ako\StoreBundle\Entity\Item $ items
* /
public function addItems(\Ako\StoreBundle\Entity\Item $ items)
{
$ this-> items [] = $ items;
}

/ **
*获取项目
*
* @return Doctrine\Common\Collections\Collection
* /
public function getItems()
{
return $ this-> items;
}
}

And:

  class Item 
{
/ **
* @ ORM\ManyToMany(targetEntity =Category,inversedBy =items ,cascade = {persist})
* @ ORM\JoinTable(name =item_category,
* joinColumns = {@ ORM\JoinColumn(name =item_id,referencedColumnName = id)},
* inverseJoinColumns = {@ ORM\JoinColumn(name =category_id,referencedColumnName =id)}
*)
* /
private $类别;

/ **
*添加类别
*
* @param Ako\StoreBundle\Entity\Category $ categories
* /
public function addCategories(\Ako\StoreBundle\Entity\Category $ categories)
{
$ this-> categories [] = $ categories;
}

/ **
*获取类别
*
* @return Doctrine\Common\Collections\Collection
* /
public function getCategories()
{
return $ this-> categories;
}
}

现在在我的控制器中:

  $ em = $ this-> getDoctrine() - > getEntityManager(); 

$ item = $ em-> getRepository('AkoStoreBundle:Item') - > find($ item_id);
$ category = $ em-> getRepository('AkoStoreBundle:Category') - > find($ category_id);

$ category-> addItems($ item);

$ em-> flush();
//再次渲染同一页面。

在此页面中,我显示了选择字段中所有项目的列表。用户可以选择一个项目,并将其添加到类别中。



属于该类别的项目列表如下所示。



当我提交表单时,所选项目被添加到Category项目列表中,如下所示,但不存储在数据库中,如果刷新页面,它将消失。



有人可以帮我吗?
提前感谢

解决方案

您的类别实体是反面



尝试更改addItems看起来像这样:

  public function addItem (\Ako\StoreBundle\Entity\Item $ item)
{
$ item-> addCategory($ this);
$ this-> items [] = $ item;
}

请注意,我将您的复数名称改为单数,因为您正在处理单个实体,而不是集合。


I have two PHP model classes named Category and Item. A Category may have many Items and an Item may belong to many Categories. I have created a ManyToMany relation to both classes:

class Category
{
    /**
     * @ORM\ManyToMany(targetEntity="Item", mappedBy="categories", cascade={"persist"})
     */
    private $items;

    /**
     * Add items
     *
     * @param Ako\StoreBundle\Entity\Item $items
     */
    public function addItems(\Ako\StoreBundle\Entity\Item $items)
    {
        $this->items[] = $items;
    }

    /**
     * Get items
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getItems()
    {
        return $this->items;
    }
}

And:

class Item
{
    /**
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="items", cascade={"persist"})
     * @ORM\JoinTable(name="item_category",
     * joinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id")},
     * inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}
     * )
     */
    private $categories;

    /**
     * Add categories
     *
     * @param Ako\StoreBundle\Entity\Category $categories
     */
    public function addCategories(\Ako\StoreBundle\Entity\Category $categories)
    {
        $this->categories[] = $categories;
    }

    /**
     * Get categories
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getCategories()
    {
        return $this->categories;
    }
}

Now in my controller:

$em = $this->getDoctrine()->getEntityManager();

$item = $em->getRepository('AkoStoreBundle:Item')->find($item_id);
$category = $em->getRepository('AkoStoreBundle:Category')->find($category_id);

$category->addItems($item);

$em->flush();
// Render the same page again.

In this page, I show the list of all items in a select field. The user can select one item, and add it to the category.

The list of items which belong to the category are shown below the form.

When the I submit the form, the selected item is added to the list of Category items, and is shown below, but it is not stored in the database, and if refresh the page, it disappears.

Can anyone please help me with this? Thanks in advance.

解决方案

Your Category entity is the inverse side of the relationship.

Try changing addItems to look like this:

public function addItem(\Ako\StoreBundle\Entity\Item $item)
    {
        $item->addCategory($this);
        $this->items[] = $item;
    }

Note that I changed your plural names to singular, since you're dealing with single entities, not collections.

这篇关于Symfony2-Doctrine:ManyToMany关系不保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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