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

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'

Answer #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

Answered By: Jannes Botis
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles