This question already has an answer here:
- Doctrine 2 limit IN subquery 1 answer
I'm trying to do a query that has a subquery with Doctrine. Right now it's giving me an error. My function in the repository is:
public function getRecentPlaylists($count = 3) {
$q = $this->_em->createQuery("
SELECT p.id,
p.featuredImage,
p.title,
p.slug,
a.firstName,
a.lastName,
a.slug as authorSlug,
(SELECT updated
FROM \Entities\Articles
ORDER BY updated DESC LIMIT 1) as updated
FROM \Entities\Playlist p
JOIN \Entities\Account a
ON p.account_id = a.id
")
->setMaxResults($count);
try{
return $q->getResult();
}catch(Exception $e){
echo $e->message();
}
}
This gives me this error:
[Semantical Error] line 0, col 210 near 'LIMIT 1) as updated FROM': Error: Class 'LIMIT' is not defined.
I'm almost giving up on Doctrine, I haven't been able to figure out how to do queries with subqueries or unions with subqueries. Any help with this function? Thanks!
What you need is to take out the inner query and make the DQL separately for that, then use the generated DQL inside
In this case you can use Doctrine's aggregate expression MAX to get the most recent date:
You don't need to use LIMIT.
You can quite easily add your own syntax to Doctrine to for example add
LIMIT 1
to subqueries, as Colin O'Dell explained on his blog.Use as follows: