DOZ DOZ - 7 months ago 24
SQL Question

Create subquery using query builder

I try to convert SQL query (which works fine) in Doctrine query builder but I do not succeed because it contains a sub query

This is my SQL Query :

FROM `navigation`
WHERE `parent_id` =
FROM `navigation`
WHERE `parent_id` = 47
AND `nav_type`= 'nav'
AND `published` = 1
AND `title` = 'Top'

And this is what I tried in my Repository :

class NavigationRepository extends EntityRepository
public function test($parentId, $type, $status, $title)
$subQuery = $this->createQueryBuilder('n2')
->where('n2.parent = :parent')
->andWhere('n2.type = :type')
->andWhere('n2.status = :status')
->andWhere('n2.title = :title')
->setParameter('parent', $parentId)
->setParameter('type', $type)
->setParameter('status', $status)
->setParameter('title', $title)

$qb = $this->createQueryBuilder('n');
$qb->expr()->eq('n.parent', '('.$subQuery.')')

return $qb

But I got this error :

QueryException: Invalid parameter number: number of bound variables
does not match number of tokens

Why ? It seems I have the right number of parameter yet...


You should call setParameter() on $qb instead of $subQuery. The DQL doesn't contain interpolated parameters, it's just a regular string.