With Symfony and Doctrine, I have an error with "eq" subquery :
It's OK, no error :
public function getForums()
{
$qb = $this->createQueryBuilder('fc');
return $qb
->innerJoin('fc.versions', 'fcv')
->innerJoin('fc.versions', 'fcvl', 'WITH', $qb->expr()->in(
'fcvl.id',
$this->_em->createQueryBuilder()
->select('MAX(v.id)')
->from(ForumCategoryVersion::class, 'v')
->where('v.forumCategory = fc')
->getDQL()
))
->select('fc, fcv')
->getQuery()
->getResult();
}
Replace in
by eq
:
public function getForums()
{
$qb = $this->createQueryBuilder('fc');
return $qb
->innerJoin('fc.versions', 'fcv')
->innerJoin('fc.versions', 'fcvl', 'WITH', $qb->expr()->eq(
'fcvl.id',
$this->_em->createQueryBuilder()
->select('MAX(v.id)')
->from(ForumCategoryVersion::class, 'v')
->where('v.forumCategory = fc')
->getDQL()
))
->select('fc, fcv')
->getQuery()
->getResult();
}
I have this error :
[Syntax Error] line 0, col 208: Error: Expected Literal, got 'SELECT'
You need to use parenthesis () for the subquery:
->innerJoin('fc.versions', 'fcvl', 'WITH', $qb->expr()->eq(
'fcvl.id',
'(' . $this->_em->createQueryBuilder()
->select('MAX(v.id)')
->from(ForumCategoryVersion::class, 'v')
->where('v.forumCategory = fc')
->getDQL() . ')'
))
References