Extra lazy
You can peek into lazy associations .
Basically, you map all associations, as usual, and add fetch="EXTRA_LAZY" :
class CmsGroup { public $users; }
Now, Doctrine will not load the complete collection into memory on first access, but it will do specialized requests to load the parts that you really need at the moment.
So, $users->count() (or count($users) ) in the collection will cause a simple request count instead of loading the complete collection into memory.
afterload
You can use the postLoad event to determine if this object can be deleted. This postLoad event is postLoad after the entity has been created by EntityManager, therefore, when the entity has been loaded.
Add the unmapped property ( $isDeletable ) to an object that stores whether the object can be deleted or not.
Create an object listener that listens for the postLoad event. The listener may have an EntityManager, DBAL Connection, or something else that is being entered. With this dependency, you can execute any query you want and use the result to set $isDeletable .
The result is one additional request when the object is loaded, after which the entity “knows” whether it is deleted or not.
An example of using the postLoad event can be found in the Cookbook entry in the strategy template.
Note that under conditions that determine whether it is deleted or not changed, the value of $isDeletable may become incorrect. To solve this problem, you can track these conditions:
Keep track of
Add the associated property ( $isDeletable ) to an object that stores whether the object can be deleted or not. It will probably start with true .
When something is added to the association, which means the object is no longer being deleted, set $isDeletable to false .
When something is removed from the association, which means the object is deleted again, set $isDeletable to true .
In other words: every time you change, you track whether the object is deleted or not.
This way you will not need additional queries.
Here 's a cookbook entry in summary fields that explains this concept very well.