简体   繁体   中英

Doctrine join one-to-many and hydrate collection

I have a one-to-many relationship registered with Doctrine. When I run the DQL:

SELECT g FROM App\Models\Group g JOIN g.users u

I expect the Groups in the result to have the $users collection filled. However, this collection is empty, and when I access it Doctrine runs another query to hydrate it.

How can I fetch the Groups and their Users in one query? I have seen the fetch join option, but I don't like it, because then I have to recombine the groups and the users myself, which is difficult and messy.

Juan was right, I just needed to add SELECT g, u . The key thing for me was that this works:

SELECT g, u FROM App\Models\Group g JOIN g.users u

but this doesn't:

SELECT g, u FROM App\Models\Group g JOIN App\Models\Users u

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