教义多对多选择

时间:2013-11-25 11:35:46

标签: php doctrine-orm

我有两个实体,分组和用户:

class Group
{
    /**
 * @ORM\ManyToMany(targetEntity="Group", inversedBy="groups")
 * @ORM\JoinTable(name="admin_group_user",
 *      joinColumns={@ORM\JoinColumn(name="fk_group", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="fk_user", referencedColumnName="id")}
 *      )
 */
protected $users;
...
}

class User
{
/**
 * @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
 * @ORM\JoinTable(name="admin_group_user",
 *      joinColumns={@ORM\JoinColumn(name="fk_user", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="fk_group", referencedColumnName="id")}
 *      )
 */
protected $groups;
 ...
 }

我想获得结果 组1具有用户A,用户B,用户C. 组2具有用户D,用户E,用户F.

通常类似

SELECT admin_group.id AS group_id, admin_group.name, agu.fk_user, fu.username
FROM admin_group
 JOIN admin_group_user agu ON (admin_group.id = agu.fk_group)
 JOIN front_user fu ON (agu.fk_user = fu.id);

有人知道如何使用Doctrine实现这一目标吗?

1 个答案:

答案 0 :(得分:1)

关于doctrine上的多对多双向映射的文档可帮助您解决问题:

http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional