Doctrine return error with “eq”, no with “in”

给你一囗甜甜゛ 提交于 2021-01-29 08:33:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!