dqdmvg7332 2012-08-01 22:23
浏览 8
已采纳

获取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()

  • 写回答

1条回答 默认 最新

  • douhun8647 2012-08-02 09:30
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法