ZF2 Doctrine: When to flush ObjectManager

2019-07-18 05:16发布

when is the best "time" to flush the Entity/Object-manager? Should it be after every persist operation? Or should it be run once on e.g. postDispatch?

2条回答
爷的心禁止访问
2楼-- · 2019-07-18 05:29

Ideally, once at the end of the request. But if you're working with too many entities, than it's better do flush soon as you can, and do not let the unit of work to be overloaded with entities. This is when things can get very weird, because the problem with the spl_object_hash doesnt identify all those objects with unicity.

Use the clear() too, as soon as you're done with some entities and will started with others.

查看更多
爷的心禁止访问
3楼-- · 2019-07-18 05:35

Running it after every persist is an antipattern actually. Ideally, you should run it once at the end of the request.

I would not put it in a postDispatch handler, because that means it will run after every request, and that is going to be costly performance wise on, for example, list pages, where you list entities with many relations, because Doctrine will have to examine many entities for changes.

Put it at the end of actions that modify data.

查看更多
登录 后发表回答