Listening to Doctrine and Subscriber

I work within Symfony2 and wonder when it will be possible to use a Doctrine subscriber against a listener. The doctrine for listeners is very clear, but subscribers are quite obscured. Symfony cookbook entry .

+56
symfony doctrine2
May 24 '12 at 2:32
source share
5 answers

From my point of view, there is only one significant difference:

  • A listener is signed indicating the events at which he is listening.
  • The Subscriber has a way to tell the dispatcher what events he is listening to.

This may seem like a big difference, but if you think about it, there are some cases where you want to use one over the other:

  • You can assign one listener to many dispatchers with different events, since they are installed during registration. You only need to make sure that each method is installed in the listener.
  • You can change the events that a subscriber is registered at runtime, and even after registering a subscriber, by changing the return value of getSubscribedEvents (think about the time when you listen to a very noisy event and you only want to execute something once)

There may be other differences that I do not know about!

+63
Jun 14 2018-12-12T00:
source share

I do not know if this was done by accident or intentionally. But subscribers have a higher priority than listeners - https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPass.php#L73-L98

From the doctrine, it doesn't matter what it is (listener or subscriber), in the end both are registered as listeners - https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/EventManager.php#L137 -L140

This is what I noticed.

+8
Apr 15 '14 at 10:31
source share

You must use an event subscriber if you want to deal with several events in the same class, for example, in this page of the symfony2 doc article , you can note that the event listener can only manage one event, but lets say that you want to deal with several events for one object, prePersist, preUpdate, postPersist, etc. .... if you use an event listener, you will have to encode several event listeners, one for each event, but if you go with an event subscriber, you if you need to encode one class on a suspicious event, look that with the event subscriber you can manage several events in one class, well, here’s how I use it, I prefer to focus the code on what the model business needs, one example of this might be that you want to handle several globaly life cycle events only for a group of your entities, to do this, you can encode the parent class and define these global methods in it, and then make your objects inherit this class, Later in your case susbcriber signed by each eve nt you want, prePersist, preUpdate, postPersist, etc., and then ask the parent class and do these global methods.

+7
Nov 21 '13 at 16:09
source share

Both allow you to do something on a specific pre / post persist event, etc.

However, listeners only allow you to perform the behavior encapsulated inside your object. Thus, the example can update the mark with the mark "date_edited".

If you need to go beyond your facility, you will need a subscriber. A good example would be to call an external API, or if you need to use / check data that is not directly related to your object.

+5
May 24 '12 at 15:58
source share

Another important thing: Doctrine EventSubscribers do not allow you to set priority.

Read more about this release here.

+2
Mar 11 '16 at 16:55
source share



All Articles