简体   繁体   中英

Symfony 1.4, Doctrine: how to get the lastest “joined” record?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM