Guide to doctrine orm events 

published 22 October 2020

I noticed that every time i talk with a college about doctrine events there is confusion about which event does what. So I tried to lay it out here.

You can look at the official documentation for the newest info but here is the list of doctrine events in the order they are called.

All events are triggered in the $em->flush(); method in this order. Or even more precisely in Doctrine\ORM\UnitOfWork::commit method. Take a look there if you really want to know what you are doing.

Knowing this now, the 2 events you are probably most interested in are onFlush and postFlush. All other events have only very specific use cases.

what about prePersist and preDelete events? 

prePersist and preRemove are called during $em->persist() and $em->remove() respectively if they are explicitly called with that entity. This makes these events really awkward to deal with since they could be called during flush (though cascade while computing the change set) but also before the entity is even fully filled. Think of this situation:

$entity = new Entity();
$em->persist($entity); // calls prePersist
$entity->setName("example"); // this name was not known in pre persist

Even stranger, you can call the prePersist event multiple times