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.