-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
DoctrinehasPRA Pull Request has already been submitted for this issue.A Pull Request has already been submitted for this issue.
Milestone
Description
In the doc we say:
There are different ways to listen to these Doctrine events:
- Lifecycle callbacks, they are defined as public methods on the entity classes and
they are called when the events are triggered;- Lifecycle listeners and subscribers, they are classes with callback
methods for one or more events and they are called for all entities;- Entity listeners, they are similar to lifecycle listeners, but they are
called only for the entities of a certain class.These are the drawbacks and advantages of each one:
- Callbacks have better performance because they only apply to a single entity
class, but you can't reuse the logic for different entities and they don't
have access to :doc:Symfony services </service_container>
;- Lifecycle listeners and subscribers can reuse logic among different entities
and can access Symfony services but their performance is worse because they
are called for all entities;- Entity listeners have the same advantages of lifecycle listeners and they have
better performance because they only apply to a single entity class.
I want to focus on the very last point:
Entity listeners have the same advantages of lifecycle listeners and they have
better performance because they only apply to a single entity class.
So it means this one doesn't have any drawback, doesn't ?
So to me, I would use the following pattern
- Very simple code for one entity, without extra dependencies => Lifecycle callbacks
- More complexe code for one entity, with dependencies => Entity listeners
- Shared code between different kind of entities => Lifecycle listeners
And I would not talk about Lifecycle subscriber at all, since it's deprecated
Metadata
Metadata
Assignees
Labels
DoctrinehasPRA Pull Request has already been submitted for this issue.A Pull Request has already been submitted for this issue.