使用Symfony和Doctrine,我在"eq"子查询中出现错误:
还好,没有错误:
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();
}
用eq
替换in
:
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();
}
我有这个错误:
〔语法错误〕第0行,第208列:错误:需要Literal,得到了SELECT
您需要使用括号((用于子查询:
->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() . ')'
))
参考
- Mysql equal子查询
- 用教义做一件事