在Symfony2中保存多对多关系到数据库 [英] Saving Many to Many relationship to database in Symfony2

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

问题描述

在我的Symfony2项目中,我有两个相关的实体:用户和收藏夹。他们有一个多对多的关系。

In my Symfony2 project I have two related entities: Users and Favorites. They have a many-to-many relationship.

我的应用程序工作原理如下:
在我的Twig页面中,我有一些项目,按钮'添加到收藏夹'。当您单击按钮时,我的控制器将item_id保存在收藏夹列中。但是我想保存
的用户添加项目到他的收藏夹,这里我的应用程序失败。

My application works as follows: In my Twig-page I have an few items with a button 'Add to Favorites'. When you click the button my controller saves the item_id in the Favorites column. But then I want to save the user who added the item to his favorites and here my application fails.

用户和收藏夹存在,但连接列之间用户和收藏夹保持为空。
我也不会收到任何错误。

The User and the favorite exist but the joincolumn between Users and Favorites remains empty. I also don't receive any kind of errors.

这是我的代码:

实体用户

class Users implements AdvancedUserInterface
{
    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="Favorites", inversedBy="user", cascade={"persist"})
     * @ORM\JoinTable(name="user_has_favorite",
     *   joinColumns={
     *     @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="favorite_id", referencedColumnName="favorite_id")
     *   }
     * )
     */
    private $favorite;

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

    public function addFavorite(\Geo\CityTroopersBundle\Entity\Favorites $favorite)
    {
        $this->favorite[] = $favorite;

        return $this;
    }
...

实体收藏

class Favorites
{

    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="Users", mappedBy="favorite", cascade={"persist"})
     */
    private $user;

    public function __construct()
    {
        $this->user = new \Doctrine\Common\Collections\ArrayCollection();
    }
    public function addUser(\Geo\CityTroopersBundle\Entity\Users $user)
    {
        $this->user[] = $user;    
        return $this;
    }

我的控制器

public function showNewsAction()
    {
        $request = $this->get('request');    
        $itemId=$request->request->get('itemId');
        if($itemId != NULL)
        {
        //MAKE NEW FAVORITE AND ADD TO DATABASE LINKED WITH ITEM
        $favorite = new Favorites();
        $favorite->setItemId($itemId);

         //LINK FAVORITE ID WITH USER ID IN JOINCOLUMN
        $userId = 6;
        $em = $this->getDoctrine()->getEntityManager();

        $user = $em->getRepository('GeoCityTroopersBundle:Users')->find($userId);

        $favorite->addUser($user);
        $em->persist($favorite); 

        //I TRIED THIS TOO, BUT IT FAILED
        /*$user->addFavorite($favorite);
        $em->persist($user);*/

        $em->flush();


推荐答案

对于原则多对多关系,您需要调用两个添加方法

You were close there. For doctrine many-to-many relationships, you need to call both add methods

$favorite->addUser($user);
$user->addFavorite($favorite);

$em->persist($favorite); 
$em->persist($user);
$em->flush();

这应该可以做到。在文档中这个,但不要太明确地提及。不知道为什么要么因为很多人遇到这个(包括我自己)。

This should do the trick. In the docs they do this, but don't mention it too explicitly. Not sure why either because lots of people run into this (myself included).

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

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