Take the 2-minute tour ×

I have a basic table article linked to a table category by id (classic), I would like to make a doctrine request to retrieve only 5 articles by category (for all categories)

This request return me all articles of categories, I just want 5 for each

public function getArticlesAndCategs(){
    $qb = $this->createQueryBuilder('c')
                ->leftJoin('c.articles', 'a')
    return $qb->execute();                

Can you help me for that? thank you

share|improve this question
sqlfiddle.com/#!2/075e1/5 –  Dawid Sajdak Feb 1 '13 at 15:13
no thanks ! i know how to do in simple sql, but i want a doctrine querybuilder request. –  rudak Feb 1 '13 at 15:28
==> i want a doctrine querybuilder request <== stop to paste anything please –  rudak Feb 1 '13 at 16:12
This is why ORMs get a bad name. You're trying to do a complicated database query with something designed for CRUD and ID-based joins and lookups. Stop and think about the value of what you're doing, and why exactly you "want" a doctrine request, instead of writing your result in SQL and just using a createQuery call instead. –  Kyle Hale Feb 24 '13 at 16:51

1 Answer 1

up vote 1 down vote accepted

This is no easy task unfortunately. At least I haven't found a satisfying solution yet (satysfying for me ;) ).

Three things you can do:

  1. Iterate through the categories and retrieve 5 posts for each category. This of course cause that many db request as many you have categories.

  2. Return all the categories with joined posts and iterate inside php. The drawback - you'll return all the posts, so if you have many, this can mean a lot of memory used.

  3. If you don't have to stick to Doctrine you can try the solution from here: mySQL Returning the top 5 of each category

share|improve this answer

Your Answer


