0

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'

Gaylord.P
  • 1,539
  • 2
  • 24
  • 54
  • Use `in` then, instead of `eq`. What is your question? – Cid Dec 04 '18 at 08:01
  • Actually, on a 2nd thought, you could create an enhancement issue on doctrine `s github repo and ask to make the use of parenthesis optional. It is weird to be supported on one case and not the other. – Jannes Botis Dec 08 '18 at 13:06

1 Answers1

0

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

Jannes Botis
  • 11,154
  • 3
  • 21
  • 39