Doctrine 2 - Outer join query

一笑奈何 提交于 2019-12-24 18:10:54

问题


In the context of the SonataAdminBundle / SonataUserBundle, I'm using the query builder to add static filters to the "list" query :

With this query, I get only users in the group "Juge", the query works well :

$query
  ->leftJoin( $query->getRootAlias().'.groups', 'g')
  ->andWhere( 'g.name = :group_name' )
  ->setParameter('group_name', 'Juge'); 

In an other Admin class, i want to do the oposite of this query : get the users who ARE NOT in the "Juge" group. How can I perform this? There is not outerJoin function in doctrine 2 right?


回答1:


I think you want to do

$query
  ->leftJoin( $query->getRootAlias().'.groups', 'g',
    Expr\Join::WITH, 'g.name = :group_name')
  ->where('g.name IS NULL')
  ->setParameter('group_name', 'Juge'); 

where Expr is Doctrine\ORM\Query\Expr.




回答2:


I used the following code and it works for me, its a kind of Outer Join.

$qb = $this->getEntityManager()->createQueryBuilder()
     ->select('u')
     ->from('UserBundle:User', 'u')
     ->leftJoin('u.group g WITH g.id = :groupId', false)
     ->where('g IS NULL')
     ->groupBy('u.id')
     ->setParameter('groupId', 12) 
return $qb->getQuery()->getResult();     


来源:https://stackoverflow.com/questions/16588270/doctrine-2-outer-join-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!