Getting started with events
The Doctrine Common component comes with a built-in event system. It allows dispatching and subscribing to custom events, but its main purpose is to manage entity-related events.
In Chapter 1, Getting Started with Doctrine 2, we learned about entity managers, entity states, and Unit Of Work. Entity Managers (and their underlying UnitOfWork
objects) dispatch events when the state of the entity changes and when data is stored, updated, and removed from the database. They are called lifecycle events.
Note
Doctrine also emits some events not directly related to the entity lifecycle.
Doctrine ORM provides the following bunch of lifecycle events:
preRemove
: This event occurs when the state of the entity is set toremoved
postRemove
: This event occurs after the removal of an entity's data from the databaseprePersist
: This event occurs when the state of the entity passes fromnew
tomanaged
postPersist
: This event occurs after theINSERT
SQL query has been executedpreUpdate...