Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define the order in which event listeners / subscribers are called in doctrine?

I have two event subscribers A and B. Both subscribed to the onFlush event.

I want to make sure that a::onFlush() is always called before B::onFlush(). I can't find any resource on this in the documentation.

I use symfony2 so it would be great if I could just pass the value in in the service definition.

like image 860
Jumi Avatar asked Apr 06 '15 08:04

Jumi


People also ask

What is the Order in which the event subscriber methods are called?

When an event is raised, the subscriber methods are run one at a time in no particular order. You can't specify the order in which the subscriber methods are called. You create an event subscriber method just like other methods except that you specify properties that set up the subscription to an event publisher.

What is the difference between Symfony event subscribers and doctrine event subscribers?

Doctrine event subscribers cannot return a flexible array of methods to call for the events like the Symfony event subscribercan. Doctrine event subscribers must return a simple array of the event names they subscribe to.

What does sub-subscribing to an event do?

Subscribing to an event tells the runtime that the subscriber method must be called whenever the publisher method is run, either by code (as with business and integration events) or by the system (as with trigger events).

What happens when an event is published?

When the published event is raised, the event subscriber is called and its code is run. Subscribing to an event tells the runtime that the subscriber method must be called whenever the publisher method is run, either by code (as with business and integration events) or by the system (as with trigger events).


1 Answers

Add a priority to your service tag. The higher the priority the earlier it will run.

services:
    my.listener:
        class: Acme\SearchBundle\EventListener\SearchIndexer
        tags:
            - { name: doctrine.event_listener, event: postPersist, priority: 100 }
like image 84
Twifty Avatar answered Oct 19 '22 11:10

Twifty