Doctrine 2: how do you use a subquery column (in the SELECT clause)

后端 未结 4 1851
天涯浪人
天涯浪人 2020-12-30 00:28

I\'m trying to do a simple select query with a subquery in the SELECT clause and have simply not found a way to do it. I\'ve tried with both DQL and with the QueryBuilder, n

4条回答
  •  死守一世寂寞
    2020-12-30 01:07

    For me subquery with doctrine works with this query :

    $qb->select('e.field')
       ->addSelect('(SELECT count(mv.nm)
                    FROM Clt\Bundle\MyBundle\Entity\MV mv
                    LEFT JOIN Clt\Bundle\MyBundle\Entity\M ma WITH  mv.nm=ma.nm
                    WHERE mv.ne=e.ne and ma.nm is null
                    ) AS nm'
       )
       ->from($this->_entityName, 'e')
       ->leftJoin('e.m', 'm')
       ->where($qb->expr()->eq('t.id'.$typeModule, $idElementModule));
    

    Note that in the left join you must use WITH instead of ON...

提交回复
热议问题