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

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

问题描述

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

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
{
    /**
     * @ORMManyToMany(targetEntity="Item", mappedBy="categories", cascade={"persist"})
     */
    private $items;

    /**
     * Add items
     *
     * @param AkoStoreBundleEntityItem $items
     */
    public function addItems(AkoStoreBundleEntityItem $items)
    {
        $this->items[] = $items;
    }

    /**
     * Get items
     *
     * @return DoctrineCommonCollectionsCollection 
     */
    public function getItems()
    {
        return $this->items;
    }
}

还有:

class Item
{
    /**
     * @ORMManyToMany(targetEntity="Category", inversedBy="items", cascade={"persist"})
     * @ORMJoinTable(name="item_category",
     * joinColumns={@ORMJoinColumn(name="item_id", referencedColumnName="id")},
     * inverseJoinColumns={@ORMJoinColumn(name="category_id", referencedColumnName="id")}
     * )
     */
    private $categories;

    /**
     * Add categories
     *
     * @param AkoStoreBundleEntityCategory $categories
     */
    public function addCategories(AkoStoreBundleEntityCategory $categories)
    {
        $this->categories[] = $categories;
    }

    /**
     * Get categories
     *
     * @return DoctrineCommonCollectionsCollection 
     */
    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();
// 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.

当我提交表单时,选中的item被添加到Category items的列表中,如下图所示,但是没有保存在数据库中,刷新页面就消失了.

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.

尝试将 addItems 更改为如下所示:

Try changing addItems to look like this:

public function addItem(AkoStoreBundleEntityItem $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天全站免登陆