简体   繁体   English

Doctrine 加入一对多和水合物收集

[英]Doctrine join one-to-many and hydrate collection

I have a one-to-many relationship registered with Doctrine.我在 Doctrine 注册了一对多关系。 When I run the DQL:当我运行 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.我希望结果中的 Groups 填充了 $users 集合。 However, this collection is empty, and when I access it Doctrine runs another query to hydrate it.然而,这个集合是空的,当我访问它时 Doctrine 运行另一个查询来补充它。

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.我见过 fetch join 选项,但我不喜欢它,因为我必须自己重新组合组和用户,这既困难又混乱。

Juan was right, I just needed to add SELECT g, u .胡安是对的,我只需要添加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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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