Search code examples
phpdatabasesymfonysubquerydql

Symfony DQL not executing subquery


I'm quite new to DQL and I've got a problem with subqueries. I need to get a list of videoIDs that got added in query2 after the last execution of an optimization.

Symfony is throwing me this exception:

[Syntax Error] line 0, col -1: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got end of string.

I think the error is generated by the subquery. Do you know why? Or do you have another way - maybe simpler - of doing this?

This is my code:

public function showOptimizable(){
    $query = $this->em->createQuery('
        SELECT e.videoID
        FROM AppBundle:Query2 e
        WHERE e.timestamp < (
                             SELECT MAX(o.lastOptimization)
                             FROM AppBundle:Optimization
                            )
        GROUP BY e.videoID HAVING COUNT(e.videoID) < 3
        ORDER BY e.videoID DESC
         ');

    return $query->getResult();
}

This is query2:

  CREATE TABLE `query2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `query` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `timestamp` datetime NOT NULL,
  `ip` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `uAgent` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `videoID` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `trackTitle` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `present` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNIQ_2D293848A5D6E63E` (`timestamp`)
)

And this is optimization:

CREATE TABLE `optimization` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lastOptimization` datetime NOT NULL,
  `optimizedElements` int(11) NOT NULL,
  PRIMARY KEY (`id`)
)

Solution

  • The alias "o" is missing. Try this:

    (
      SELECT MAX(o.lastOptimization)
      FROM AppBundle:Optimization o
    )