WARNING: You are browsing the documentation for version 1.2 which is not maintained anymore. If some of your projects are still using this version, consider upgrading.

Doctrine PHPCR-ODM Events

1.2 version
Maintained Unmaintained
1.1

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:

  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    services:
        acme_search.listener.search:
            class: Acme\SearchBundle\EventListener\SearchIndexer
                tags:
                    - { name: doctrine_phpcr.event_listener, event: postPersist }
    
        acme_search.subscriber.fancy:
            class: Acme\SearchBundle\EventSubscriber\MySubscriber
                tags:
                    - { name: doctrine_phpcr.event_subscriber }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <!-- src/Acme/SearchBundle/Resources/config/services.xml -->
    <?xml version="1.0" ?>
    <container xmlns="http://symfony.com/schema/dic/services">
        <services>
            <service id="acme_search.listener.search"
                     class="Acme\SearchBundle\EventListener\SearchIndexer">
                <tag name="doctrine_phpcr.event_listener" event="postPersist" />
            </service>
            <service id="acme_search.subscriber.fancy"
                     class="Acme\SearchBundle\EventSubscriber\MySubscriber">
                <tag name="doctrine_phpcr.event_subscriber" />
            </service>
        </services>
    </container>
    
  • PHP
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    $container
        ->register(
            'acme_search.listener.search',
            'Acme\SearchBundle\EventListener\SearchIndexer'
        )
        ->addTag('doctrine_phpcr.event_listener', array(
            'event' => 'postPersist',
        ))
    ;
    
    $container
        ->register(
            'acme_search.subscriber.fancy',
            'Acme\SearchBundle\EventSubscriber\FancySubscriber'
        )
        ->addTag('doctrine_phpcr.event_subscriber', array(
            'event' => 'postPersist',
        ))
    ;
    

Tip

Doctrine event subscribers (both ORM and PHPCR-ODM) can not return a flexible array of methods to call like the Symfony event subscriber can do. 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.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.