QueryBuilder:如何编写subselect子句

时间:2017-05-10 12:46:17

标签: doctrine-query

我无法将普通的SQL子句转换为Doctrine查询。 我在下面的普通SQL中添加了该子句:

$query = $doc->getEntityManager ()
 ->createQueryBuilder ()
 ->select ( 'r')
 ->from ( 'AppBundle:CFormResponse', 'r' )
 ->where ( 'r.formId = :id AND NOT (select f.private from cform f where f.FORM_ID = :id) ' )
 ->setParameter ( 'id', $formId )->getQuery ();
$result = $query->getResult();

我将此子句转换为DQL的尝试如下(到目前为止):

$qb = $doc->getEntityManager ()->createQueryBuilder ();
$query = $qb
    ->select ( 'r')
    ->from ( 'AppBundle:CFormResponse', 'r' )
    ->where ( $qb->expr()->andX(
            $qb->expr()->eq('r.formId', ':id'),
            $qb->expr()->addSelect('(SELECT f.private
                FROM AppBundle:CForm f
                WHERE f.formId = :id)'
            )
            ) )
    ->setParameter ( 'id', $formId )->getQuery ();
$result = $query->getResult();

addSelect表示我被困的位置。 任何人都可以指出我如何做到这一点的方向?

1 个答案:

答案 0 :(得分:0)

基本上,您需要将not添加到addSelect

$query = $qb
    ->select ( 'r')
    ->from ( 'AppBundle:CFormResponse', 'r' )
    ->where ( $qb->expr()->andX(
            $qb->expr()->eq('r.formId', ':id'),
            $qb->expr()->not(
                $qb->expr()
                ->select(`f.private`)
                ->from('AppBundle:CForm', 'f')
                ->where('f.formId = :id'))
            )
            ) )
    ->setParameter ( 'id', $formId )->getQuery ();