'where not in' query with doctrine query builder

前端 未结 2 972
遇见更好的自我
遇见更好的自我 2020-12-14 15:55

Im trying to reproduce this query:

SELECT * FROM `request_lines`
where request_id not in(
select requestLine_id from `asset_request_lines` where asset_id = 1         


        
2条回答
  •  难免孤独
    2020-12-14 16:23

    You need to use query builder expressions, and this means you need access to the query builder object. Also, the code is easier to write if you generate the subselect list ahead of time:

    $qb = $em->createQueryBuilder();
    
    $nots = $qb->select('arl')
              ->from('$MineMyBundle:MineAssetRequestLine', 'arl')
              ->where($qb->expr()->eq('arl.asset_id',1))
              ->getQuery()
              ->getResult();
    
    $linked = $qb->select('rl')
                 ->from('MineMyBundle:MineRequestLine', 'rl')
                 ->where($qb->expr()->notIn('rl.request_id', $nots))
                 ->getQuery()
                 ->getResult();
    

提交回复
热议问题