Symfony2-Doctrine: ManyToMany relation is not saved to database

梦想与她 提交于 2019-11-26 04:41:13

问题


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.


回答1:


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.




回答2:


I had the same problems...I think you forgot

$category->addItems($item);
$em->persist($category);
$em->flush();


来源:https://stackoverflow.com/questions/7044864/symfony2-doctrine-manytomany-relation-is-not-saved-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!