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.


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 to removed
  • postRemove ...

Get Persistence in PHP with Doctrine ORM now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.