symfony2 Doctrine Entity Repository Make a Query with a Sub-Query


As example, only for demonstrate HOW-TO use a subquery select statement inside a select statement, suppose we what to find all user that not yet have compile the address (no records exists in the address table):

 // get an ExpressionBuilder instance, so that you
$expr = $this->_em->getExpressionBuilder();

// create a subquery in order to take all address records for a specified user id
$sub = $this->_em->createQueryBuilder()
    ->from($this->_addressEntityName, 'a')
    ->where('a.user =');

$qb = $this->_em->createQueryBuilder()
    ->from($this->_userEntityName, 'u')

return $qb->getQuery()->getResult();