From 601450e25e2df36409bfacac5fae7d8f54fa9c66 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Wed, 27 Jun 2018 21:17:27 +0200 Subject: [PATCH 1/4] Update event_listeners_subscribers.rst --- doctrine/event_listeners_subscribers.rst | 53 ++---------------------- 1 file changed, 4 insertions(+), 49 deletions(-) diff --git a/doctrine/event_listeners_subscribers.rst b/doctrine/event_listeners_subscribers.rst index 694c9dbd1f8..17925ae0ee0 100644 --- a/doctrine/event_listeners_subscribers.rst +++ b/doctrine/event_listeners_subscribers.rst @@ -186,57 +186,12 @@ interface and have an event method for each event it subscribes to:: For a full reference, see chapter `The Event System`_ in the Doctrine documentation. -Lazy loading for Event Listeners +Performance considerations -------------------------------- -One subtle difference between listeners and subscribers is that Symfony can load -entity listeners lazily. This means that your listener class will only be fetched +One subtle difference between listeners and subscribers is that Symfony will load +entity listeners lazily by default as of Symfony 4.2. This means that your listener class will only be fetched from the service container (and thus be instantiated) once the event it is linked to actually fires. -Lazy loading might give you a slight performance improvement when your listener -runs for events that rarely fire. Also, it can help you when you run into -*circular dependency issues* that may occur when your listener service in turn -depends on the DBAL connection. - -To mark a listener service as lazily loaded, just add the ``lazy`` attribute -to the tag like so: - -.. configuration-block:: - - .. code-block:: yaml - - services: - App\EventListener\SearchIndexer: - tags: - - { name: doctrine.event_listener, event: postPersist, lazy: true } - - .. code-block:: xml - - - - - - - - - - - - .. code-block:: php - - use App\EventListener\SearchIndexer; - - $container - ->autowire(SearchIndexer::class) - ->addTag('doctrine.event_listener', array('event' => 'postPersist', 'lazy' => 'true')) - ; - -.. note:: - -   Marking an event listener as ``lazy`` has nothing to do with lazy service - definitions which are described :doc:`in their own section ` - -.. _`The Event System`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html -.. _`the Doctrine Documentation`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners +So whenever possible it is preferable to use entity listeners instead of subscribers. From d8bb8492bb0e2ac95696aebef348ced0a69972ab Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 28 Jun 2018 16:54:02 +0200 Subject: [PATCH 2/4] Reworded and added the versionadded directive --- doctrine/event_listeners_subscribers.rst | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/doctrine/event_listeners_subscribers.rst b/doctrine/event_listeners_subscribers.rst index 17925ae0ee0..41009fdfe7d 100644 --- a/doctrine/event_listeners_subscribers.rst +++ b/doctrine/event_listeners_subscribers.rst @@ -186,12 +186,17 @@ interface and have an event method for each event it subscribes to:: For a full reference, see chapter `The Event System`_ in the Doctrine documentation. -Performance considerations --------------------------------- +Performance Considerations +-------------------------- -One subtle difference between listeners and subscribers is that Symfony will load -entity listeners lazily by default as of Symfony 4.2. This means that your listener class will only be fetched -from the service container (and thus be instantiated) once the event it is linked -to actually fires. +One important difference between listeners and subscribers is that Symfony loads +entity listeners lazily. This means that the listener classes are only fetched +from the service container (and instantiated) if the related event is actually +fired. -So whenever possible it is preferable to use entity listeners instead of subscribers. +That's why it is preferable to use entity listeners instead of subscribers +whenever possible. + +.. versionadded:: 4.2 + The default lazy behavior of Doctrine entity listeners was introduced in + Symfony 4.2. From b56ef9eaf193031fad4cbd27698bf8b4c40a4b78 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 28 Jun 2018 17:18:00 +0200 Subject: [PATCH 3/4] Update event_listeners_subscribers.rst --- doctrine/event_listeners_subscribers.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doctrine/event_listeners_subscribers.rst b/doctrine/event_listeners_subscribers.rst index 41009fdfe7d..ae22a5d04cb 100644 --- a/doctrine/event_listeners_subscribers.rst +++ b/doctrine/event_listeners_subscribers.rst @@ -198,5 +198,4 @@ That's why it is preferable to use entity listeners instead of subscribers whenever possible. .. versionadded:: 4.2 - The default lazy behavior of Doctrine entity listeners was introduced in - Symfony 4.2. + Starting from Symfony 4.2, Doctrine entity listeners are lazy by default. From 4409fd65b77062b337a18501d8532510b402a7f6 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 29 Jun 2018 08:45:52 +0200 Subject: [PATCH 4/4] Explain that lazy listeners are mandatory, not default --- doctrine/event_listeners_subscribers.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doctrine/event_listeners_subscribers.rst b/doctrine/event_listeners_subscribers.rst index ae22a5d04cb..5038f16cdb3 100644 --- a/doctrine/event_listeners_subscribers.rst +++ b/doctrine/event_listeners_subscribers.rst @@ -198,4 +198,5 @@ That's why it is preferable to use entity listeners instead of subscribers whenever possible. .. versionadded:: 4.2 - Starting from Symfony 4.2, Doctrine entity listeners are lazy by default. + Starting from Symfony 4.2, Doctrine entity listeners are always lazy. In + previous Symfony versions this behavior was configurable.