I am using this:
my.listener:
class: Acme\SearchBundle\Listener\SearchIndexer
tags:
- { name: doctrine.event_listener, event: postPersist }
Now if I try to listen for two events like this:
- { name: doctrine.event_listener, event: postPersist, preUpdate }
it gives an error.
You can add many event handlers to one element. You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements.
Unfortunately, you can't pass in multiple events to a single listener like you might in jQuery and other frameworks. For example, you cannot do this: document. addEventListener('click mouseover', function (event) { // do something... }, false);
The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them.
It allows to send events as MQ messages and process them async.
I think you can do like this:
my.listener:
class: Acme\SearchBundle\Listener\SearchIndexer
tags:
- { name: doctrine.event_listener, event: postPersist }
- { name: doctrine.event_listener, event: preUpdate }
You need an event subscriber instead of an event listener.
You'd change the service tag to doctrine.event_subscriber
, and your class should implement Doctrine\Common\EventSubscriber
. You need to define a getSubscribedEvents
to satisfy EventSubscriber
which returns an array of events you want to subscribe to.
ex
<?php
namespace Company\YourBundle\Listener;
use Doctrine\Common\EventArgs;
use Doctrine\Common\EventSubscriber;
class YourListener implements EventSubscriber
{
public function getSubscribedEvents()
{
return array('prePersist', 'onFlush');
}
public function prePersist(EventArgs $args)
{
}
public function onFlush(EventArgs $args)
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With