0
votes

An user has more tasks, 1 : N connection.

Visually:

admin
 task1 2011/01/01 00:00:01
 task2 2011/01/01 00:00:04
 task3 2011/01/01 00:00:02
user2
 task1 2011/03/01 00:01:01
 task2 2011/03/01 00:01:04
 task3 2011/03/01 00:01:02

(see the schema from my previous question)

How to left join the user and the last "task", not get all of them?

The matter is I dont know how to do it with pure SQL... I saw some example here, but they are not working.

1

1 Answers

6
votes

If we consider that you have the user id inside $user_id and according to your schema in a previous question, you can try this:

$q = Doctrine_Query::create()
    ->select('u.*, t.*')
    ->from('User u')
    ->leftJoin('u.Task t')
    ->where('t.id IN (SELECT t2.id FROM Task t2 WHERE t2.owner_id = ? ORDER BY t2.id DESC LIMIT 1)', array($user_id));
$res = $q->execute();

Here is the documentation for subqueries in Doctrine.