php 使用 Doctrine 和 Symfony2 查询多对多关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26549120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Query on a many-to-many relationship using Doctrine with Symfony2
提问by Ema.jar
I'm triyng to understand how the many to many relationship works with Doctrine and Symfony2.
我正在尝试了解多对多关系如何与 Doctrine 和 Symfony2 一起工作。
I've recreated the example shown in the official documentation (goo.gl/GYcVE0) and i have two Entity Classes: Userand Groupas you can see below.
我重新创建了官方文档 (goo.gl/GYcVE0) 中显示的示例,并且我有两个实体类:User和Group,如下所示。
<?php
/** @Entity **/
class User
{
// ...
/**
* @ManyToMany(targetEntity="Group", inversedBy="users")
* @JoinTable(name="users_groups")
**/
private $groups;
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
/** @Entity **/
class Group
{
// ...
/**
* @ManyToMany(targetEntity="User", mappedBy="groups")
**/
private $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
If i update my DB i get this MySQL Schema:
如果我更新我的数据库,我会得到这个 MySQL 架构:
CREATE TABLE User (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE users_groups (
user_id INT NOT NULL,
group_id INT NOT NULL,
PRIMARY KEY(user_id, group_id)
) ENGINE = InnoDB;
CREATE TABLE Group (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE users_groups ADD FOREIGN KEY (user_id) REFERENCES User(id);
ALTER TABLE users_groups ADD FOREIGN KEY (group_id) REFERENCES Group(id);
The problem is that in Symfony2 I need the Entityto generate a query and in this case I don't have an Entity associated to the table users_group
because this table is created automatically by the framework.
问题是在 Symfony2 中我需要实体来生成查询,在这种情况下我没有与表关联的实体,users_group
因为该表是由框架自动创建的。
So, how can I retrieve the information related to this relationship table? For example I need to get all the Users in a Group which are the users that have an id that appears in the table users_group
.
那么,如何检索与此关系表相关的信息呢?例如,我需要获取组中的所有用户,这些用户是具有出现在表中的 id 的用户users_group
。
How can I do that using the DQL, QueryBuilder or other methods?
我如何使用 DQL、QueryBuilder 或其他方法来做到这一点?
Thanks a lot.
非常感谢。
回答by M Khalid Junaid
You can write a join DQL query as below
您可以编写一个连接 DQL 查询,如下所示
$em = $this->getContainer()->get('doctrine')->getManager();
$repository = $em->getRepository('YourNamespaceYourBundle:User');
$query = $repository->createQueryBuilder('u')
->innerJoin('u.groups', 'g')
->where('g.id = :group_id')
->setParameter('group_id', 5)
->getQuery()->getResult();
Your mapping for groups
property in User
entity will handle join part itself you don't have to mention the junction table in your DQL query
您groups
在User
实体中的属性映射将自行处理连接部分,您不必在 DQL 查询中提及连接表