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. 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:
$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:
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. In this page they say so, and suggest using DQL
for this kind of filtering. My problem is that I have not found how to make a QueryBuilder
object from a DQL
sentence. If you could also provide me with the right DQL
query, I would be very grateful...
Thanks for your help!
php bin/vendors install
, and it doesn't upgrade neither symfony nor doctrine... how can I upgrade? or when would those new versions going to be official??? thanks for your answer!!! – At