获取FOSUserBundle组的用户(Symfony2)

获取FOSUserBundle组的用户(Symfony2)

问题描述:

I have been struggling with this question for the past couple of hours and haven't been able to find anything.

I have configured the FOSUserBundle and have the groups activated correctly and working. However, the user-group relation is defined as follows in the User class :

/**
 * @ORM\ManyToMany(targetEntity="Netlabs\UserBundle\Entity\Group")
 * @ORM\JoinTable(name="fos_user_user_group",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
 * )
 */
protected $groups;

and in the Group class, I do not even have a $user list defined. I tried loads of "normal" things like creating a $users variable in my Group class and defining it's relation to the User object, but it is not playing nice with the FOSUserBundle (every time, it creates a NEW table to create the relation, when there is already one in place). For example:

/**
 * @ORM\ManyToMany(targetEntity="Netlabs\UserBundle\Entity\Group", inversedBy="groups", cascade={"all"})
 * 
 */
protected $users;

Any suggestions ?

Again, my main goal is : I want to be able to do Group->getUsers()

过去几个小时我一直在努力解决这个问题而且找不到任何东西。

我已配置FOSUserBundle并正确激活组并正常工作。 但是,用户组关系在User类中定义如下: p>

  / ** 
 * @ORM \ ManyToMany(targetEntity =“Netlabs \ UserBundle \ Entity \ 组“)
 * @ORM \ JoinTable(name =”fos_user_user_group“,
 * joinColumns = {@ ORM \ JoinColumn(name =”user_id“,referencedColumnName =”id“)},
 * inverseJoinColumns = {@ ORM  \ JoinColumn(name =“group_id”,referencedColumnName =“id”)} 
 *)
 * / 
 nprotected $ groups; 
  code>  pre> 
 
 

以及在组中 class,我甚至没有定义$ user列表。 我尝试了很多“普通”的东西,比如在我的Group类中创建一个$ users变量并定义它与User对象的关系,但是它与FOSUserBundle不一致(每次创建一个新表来创建关系, 当已经存在一个)。 例如: p>

  / ** 
 * @ORM \ ManyToMany(targetEntity =“Netlabs \ UserBundle \ Entity \ Group”,inversedBy =“groups”,cascade = {“ 所有“})
 * 
 * / 
保护$ users; 
  code>  pre> 
 
 

有什么建议吗? p>

同样,我的主要目标是:我希望能够进行Group-> getUsers() p> div>

In your Group class, your targetEntity must be User and not Group. Furthermore, try to add an inversedBy rule for the $groups attributs and modifie inversedBy in mappedBy for $users.

/**
 * @ORM\ManyToMany(targetEntity="Netlabs\UserBundle\Entity\Group", inversedBy="users")
 * @ORM\JoinTable(name="fos_user_user_group",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
 * )
 */
protected $groups;

And

/**
 * @ORM\ManyToMany(targetEntity="Netlabs\UserBundle\Entity\User", mappedBy="groups")
 * 
 */
protected $users;