在 Doctrine 2 中,可以在运行时更改获取模式(Eager/Lazy 等)吗? [英] In Doctrine 2 can the Fetch Mode (Eager/Lazy etc.) be changed at runtime?

查看:17
本文介绍了在 Doctrine 2 中,可以在运行时更改获取模式(Eager/Lazy 等)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想急切加载的实体,并在其他情况下延迟(甚至额外的延迟)加载.

我的映射没有在我的 YAML 中声明获取模式 - 所以它们使用默认值(延迟加载).

目前急切加载的唯一方法是手动构建 DQL - 我每次添加新实体时都需要更新它.

理想情况下,我只加载根实体并强制加载所有关联的对象.有什么办法可以做到这一点吗?

如果不是为什么(除了未实现的功能之外还有其他原因吗)?

解决方案

如果您想使用内置存储库方法(find()、findAll()),除非您急切地设置,否则您可能不走运加载您的注释.

您可能希望在某些自定义存储库的方法中使用查询构建器(或原始 DQL)来强制在您想要的位置进行预加载.是的,您必须在添加实体时更新该方法,但至少您将始终知道关于延迟/急切加载的情况,并且您只需将其全部维护在一个地方.

我想 find() 等没有一些 $eagerLoad 标志的原因是因为这些是简单任务的便捷方法.如果您想添加这样的标志,您很快就会进入需要按深度限制递归预加载的情况.您可能还必须开始担心循环引用(例如任何双向关联).

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)?

解决方案

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).

这篇关于在 Doctrine 2 中,可以在运行时更改获取模式(Eager/Lazy 等)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆