Lifecycle callbacks are an awesome feature in doctrine that help you to automatically update objects whenever you interact with them. For instance, an entity could automatically set its “created” timestamp whenever it is first persisted to the database or automatically update its “last updated” timestamp when you persist the entity.
If you handle all of this functionality inside the entity, you are absolutely sure you never have to update these flags in service or controller code, which helps you achieving “seperations of concern” and cleaner object oriented code.
As shown in the following example, the lifecycle callbacks are triggered by including the HasLifecycleCallbacks() annotation at the top of the class. Whenever the annotation “PrePersist” or “PreUpdate” is found above a method, this method will be called automatically when these events occur.
In addition to these callbacks, the following annotations / callbacks can also be used:
please see the Lifecycle Events in doctrine documentation for more information.