Doctrine PHPCR-ODM Events

Doctrine PHPCR-ODM provides an event system allowing to react to all important operations that documents have during their lifecycle. Please see the Doctrine PHPCR-ODM event system documentation for a full list of supported events.

The DoctrinePHPCRBundle provides dependency injection support for the event listeners and event subscribers.

Dependency Injection Tags

You can tag services to listen to Doctrine PHPCR-ODM events. It works the same way as for Doctrine ORM events. The only differences are:

  • use the tag name doctrine_phpcr.event_listener resp. doctrine_phpcr.event_subscriber instead of doctrine.event_listener;
  • expect the argument to be of class Doctrine\Common\Persistence\Event\LifecycleEventArgs.

To tag a service as event listener and another service as event subscriber, use this configuration:

1# app/config/services.yml services: app.phpcr_search_indexer: class: App\EventListener\SearchIndexer tags: - { name: doctrine_phpcr.event_listener, event: postPersist } app.phpcr_listener: class: App\EventListener\MyListener tags: - { name: doctrine_phpcr.event_subscriber }
2
3
4
5
6
7
8
9
10
11

Doctrine event subscribers (both ORM and PHPCR-ODM) can not return a flexible array of methods to call like the Symfony event subscriber. Doctrine event subscribers must return a simple array of the event names they subscribe to. Doctrine will then expect methods on the subscriber with the names of the subscribed events, just as when using an event listener.

You can find more information and examples of the doctrine event system in "How to Register Event Listeners and Subscribers" of the core documentation.