使用Doctrine与Symfony2对多对多关系进行查询 [英] Query on a many-to-many relationship using Doctrine with Symfony2

查看:156
本文介绍了使用Doctrine与Symfony2对多对多关系进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经重新创建了官方文档中显示的示例(goo),我很了解多少关系与Doctrine和Symfony2的工作原理。 gl / GYcVE0),我有两个实体类:用户,如下所示。

 <?php 
/ ** @Entity ** /
class User
{
// ...

/ **
* @ManyToMany(targetEntity =Group,inversedBy =users)
* @JoinTable(name =users_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();
}

// ...
}

如果我更新我的数据库,我得到这个MySQL模式:

  CREATE TABLE用户(
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组(
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);

问题是在Symfony2中,我需要 Entity 来生成查询在这种情况下,我没有与表 users_group 相关联的实体,因为此表是由框架自动创建的。


$ b $那么,如何检索与此关系表相关的信息?例如,我需要获取组中的所有用户,这些用户具有出现在表 users_group 中的ID。



如何使用DQL,QueryBuilder或其他方法?



非常感谢。

解决方案

您可以编写下面的加入DQL查询

  $ em = $这 - > getContainer() - >获得( '教义') - > getManager(); 
$ repository = $ em-> getRepository('YourNamespaceYourBundle:User');
$ query = $ repository-> createQueryBuilder('u')
- > innerJoin('uGroups','g')
- > where('g.id =:group_id')
- > setParameter('group_id',5)
- > getQuery() - > getResult();

组的映射属性用户实体将处理连接部分本身,您不必在DQL查询中提及连接表


I'm triyng to understand how the many to many relationship works with Doctrine and Symfony2.

I've recreated the example shown in the official documentation (goo.gl/GYcVE0) and i have two Entity Classes: User and Group as you can see below.

<?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:

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 Entity to 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.

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.

How can I do that using the DQL, QueryBuilder or other methods?

Thanks a lot.

解决方案

You can write a join DQL query as below

$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

这篇关于使用Doctrine与Symfony2对多对多关系进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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