ZF2 Doctrine: When to flush ObjectManager

China☆狼群 提交于 2019-12-11 04:06:51

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18719707/zf2-doctrine-when-to-flush-objectmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!