From d48cd70be624010795e5625349aa5c057a3431d7 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Fri, 23 Jun 2017 10:38:56 +0200 Subject: [PATCH 1/3] Mention lazy loading for event listeners Would have saved me two hours if I had known about this feature before. --- doctrine/event_listeners_subscribers.rst | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/doctrine/event_listeners_subscribers.rst b/doctrine/event_listeners_subscribers.rst index 911b3cb6b4a..f5ae0c1ec05 100644 --- a/doctrine/event_listeners_subscribers.rst +++ b/doctrine/event_listeners_subscribers.rst @@ -214,5 +214,60 @@ 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 +-------------------------------- + +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 +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: + my.listener: + class: AppBundle\EventListener\SearchIndexer + tags: + - { name: doctrine.event_listener, event: postPersist, lazy: true } + + .. code-block:: xml + + + + + + + + + + + + .. code-block:: php + + use AppBundle\EventListener\SearchIndexer; + use AppBundle\EventListener\SearchIndexer2; + use AppBundle\EventListener\SearchIndexerSubscriber; + + $container + ->register('my.listener', SearchIndexer::class) + ->addTag('doctrine.event_listener', array('event' => 'postPersist', 'lazy' => 'true')) + ; + +.. note:: + +   Marking an event listener as ``lazy`` has nothing to do with lazy serivce + 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 From dc7ff2c3d502f162ea5312100a30278d2708e643 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Fri, 23 Jun 2017 11:24:52 +0200 Subject: [PATCH 2/3] Fix tpyo --- doctrine/event_listeners_subscribers.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doctrine/event_listeners_subscribers.rst b/doctrine/event_listeners_subscribers.rst index f5ae0c1ec05..1db0c154a99 100644 --- a/doctrine/event_listeners_subscribers.rst +++ b/doctrine/event_listeners_subscribers.rst @@ -266,7 +266,7 @@ to the tag like so: .. note:: -   Marking an event listener as ``lazy`` has nothing to do with lazy serivce +   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 From b3ec4049640051d814d339f21f29d0ee269050e3 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 31 Jul 2017 12:59:54 +0200 Subject: [PATCH 3/3] Remove unnecessary namespace imports --- doctrine/event_listeners_subscribers.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/doctrine/event_listeners_subscribers.rst b/doctrine/event_listeners_subscribers.rst index 1db0c154a99..510b722f7ec 100644 --- a/doctrine/event_listeners_subscribers.rst +++ b/doctrine/event_listeners_subscribers.rst @@ -256,8 +256,6 @@ to the tag like so: .. code-block:: php use AppBundle\EventListener\SearchIndexer; - use AppBundle\EventListener\SearchIndexer2; - use AppBundle\EventListener\SearchIndexerSubscriber; $container ->register('my.listener', SearchIndexer::class)