views:

694

answers:

0

I have two tables "series" and "programs" with programs being many to one one series.

Series table

id | name
--------------
1  | lorem
2  | ipsum
3  | foo
4  | bar

Programs table

id | name      | series_id
---------------------
1  | program1  | 1
2  | program2  | 2
3  | program3  | 3
4  | program4  | 4
5  | program5  | 1
6  | program6  | 2
7  | program7  | 3
8  | program8  | 4
9  | program9  | 1
10 | program10 | 2
11 | program11 | 1
12 | program12 | 2

I would like in Doctrine (1.2) to get the two series with the latest program, in this case series 2, followed by series 1, see the last two rows in programs.

My guess would be:

$q = Doctrine_Query::create()->from('Series s')
                             ->leftJoin('s.Programs p')
                             ->orderBy('p.id desc')
                             ->limit(2);

but this returns series 4 and series 3. The problem is that Doctrine uses a distinct query getting the id's before doing the query where the data will be included, they use that tactic, as MySql cannot use limit in subqueries i think.

My questions, how would you get series 2 and series 1 via DQL?