我有一些我想急切加载的实体,而在其他情况下则延迟(甚至额外延迟)加载。
我的映射没有在 YAML 中声明获取模式,因此它们使用默认值(延迟加载)。
目前,急切加载的唯一方法是手动构建 DQL - 每次添加新实体时我都需要更新它。
理想情况下,我只加载根实体并强制加载所有关联的对象。我有什么办法可以做到这一点吗?
如果不是,为什么(除了未实现的功能之外还有其他原因吗)?
I have entities which I would like to eagerly load , and on other ocassions lazy (or even extra lazy) load.
My mappings have no fetch mode declared in my YAML- so they use the default (lazy loading).
Currently the only way to eagerly load is to by constructing the DQL manually - and I need to update this every time I add a new entity.
Ideally I would just load the root entity and the force eager loading all the associated objects. Is there any way I can do this?
If not why (is there a reason beyond it being an unimplemented feature)?
发布评论
评论(2)
如果您想使用内置存储库方法(find()、findAll()),除非您将内容设置为在注释中急切加载,否则您可能会运气不佳。
您可能希望在某些自定义存储库的方法中使用查询构建器(或原始 DQL)来强制在您想要的位置进行预先加载。是的,您必须在添加实体时更新该方法,但至少您将始终知道延迟/急切加载发生了什么,并且您只需将其全部维护在一处。
我想没有一些 $eagerLoad 标志来 find() 等的原因是因为这些是简单任务的便捷方法。如果您想添加这样的标志,您很快就会陷入想要按深度限制递归急切加载的情况。您可能还必须开始担心循环引用(例如任何双向关联)。
If you want to use built-in repository methods (find(), findAll()), you're probably out of luck unless you set things to eagerly load in your annotations.
You'll probably want to use the query builder (or raw DQL) in some custom repository's method to force eager loading where you want it. Yes, you'll have to update that method as you add entities, but at least you'll always know what's going on in regards to lazy/eager loading, and you'll only need to maintain it all in one place.
I suppose the reason there's not some $eagerLoad flag to find(), etc, is because those are convenience methods for simple tasks. If you wanted to add such a flag, you'd have quickly get into situations where you'd want to limit recursive eager loading by depth. You'd also probably have to start worrying about cyclical references (any bidirectional association, for instance).
您可以使用 DQL 的
setFetchMode()
方法来设置模式。请参阅文档:
https://web.archive.org/web/20120601032806/http://readthedocs.org/docs/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html
You can use
setFetchMode()
method of DQL to set mode.See the documentation:
https://web.archive.org/web/20120601032806/http://readthedocs.org/docs/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html