简体   繁体   English

Symfony 2中的Doctrine 2 - 通过关联过滤QueryBuilder

[英]Doctrine 2 in Symfony 2 - Filtering a QueryBuilder by an association

I have my two classes User and Role , and I need to make a QueryBuilder which returns a query for the users who have the ROLE_PROVIDER role. 我有两个类UserRole ,我需要创建一个QueryBuilder ,它为具有ROLE_PROVIDER角色的用户返回一个查询。 I need this for an entity form field in Symfony 2. In the Form Class definition I have the following snippet for the mentioned field: 我需要这个在Symfony 2中的实体表单字段。在表单类定义中,我有上面提到的字段的以下片段:

$builder->add('provider', 'entity', array(
    'class'    => 'ElCuadreAccountBundle:User',
    'property' => 'username',
    'query_builder' => function(UserRepository $ur) {
                         return $ur->getUsersByRoleQB('ROLE_PROVIDER');
                       },
    'required' => true,
));

And then in my Custom UserRepository I have the following function, which should return a QueryBuilder object: 然后在我的Custom UserRepository我有以下函数,它应该返回一个QueryBuilder对象:

public function getUsersByRoleQB($role) {
    $qb = $this->createQueryBuilder('u');
    return $qb->join('u.roles','r')
              ->where($qb->expr()->in('r.role',$qb->expr()->literal($role)))
              ->orderBy('u.username', 'ASC');
}

Of course this doesn't work, but i pasted it to illustrate my needs. 当然这不起作用,但我粘贴它来说明我的需求。

I've been looking around and it seems Doctrine2 does not support natively filtering by an association. 我一直在环顾四周,似乎Doctrine2不支持通过关联进行原生过滤。 In this page they say so, and suggest using DQL for this kind of filtering. 这个页面中他们这么说,并建议使用DQL进行这种过滤。 My problem is that I have not found how to make a QueryBuilder object from a DQL sentence. 我的问题是我还没有找到如何从DQL语句中创建QueryBuilder对象。 If you could also provide me with the right DQL query, I would be very grateful... 如果您也可以为我提供正确的DQL查询,我将非常感激......

Thanks for your help! 谢谢你的帮助!

where should actually do what you want. 哪里应该实际做你想要的。 You just have the syntax wrong for 'in': 您只需要'in'的语法错误:

This 这个

->where($qb->expr()->in('r.role',$qb->expr()->literal($role)))

Should be 应该

->where($qb->expr()->in('r.role',$role))

I know it may seem a bit strange but since prepared statements do not directly support arrays, parameters to IN clauses always have to be escaped individually (which doctrine does for you). 我知道它看起来有点奇怪但是由于预处理语句不直接支持数组,因此IN子句的参数总是必须单独转义(这个学说适合你)。 Hence the syntax is a bit different then say for an eq expression where literal is required. 因此,对于需要文字的eq表达式,语法有点不同。

You do raise a good question because I have needed to filter by association. 你提出了一个很好的问题,因为我需要按关联过滤。 I think D2.2 will allow this out of the box. 我认为D2.2将允许开箱即用。 I have not really tried it but I suspect that 我没有真正尝试过,但我怀疑

$dql = 'a,b FROM whatever...'; // Don't start with SELECT
$qb->select($dql); 
return $qb;

will actually work without specifying any other parts as long as your leave the actual 'SELECT' string out of the $dql. 只要您将实际的'SELECT'字符串保留在$ dql之外,它实际上可以在不指定任何其他部分的情况下工作。 Untested. 未经测试。

even simpler still you can do: 你甚至可以做得更简单:

->where('r.role IN (:role)' )
->setParameter( 'role', $role );

I find this much more legible than adding the $qb->expr()... 我发现这比添加$ qb-> expr()更容易辨认......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM