问题
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'
回答1:
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
- Mysql equal subquery
- Doing a where in with doctrine
来源:https://stackoverflow.com/questions/53608128/doctrine-return-error-with-eq-no-with-in