Skip to content

Commit a64d407

Browse files
committed
minor symfony#8080 Mention lazy loading for Doctrine event listeners (mpdude)
This PR was squashed before being merged into the 2.7 branch (closes symfony#8080). Discussion ---------- Mention lazy loading for Doctrine event listeners Would have saved me two hours if I had known about this feature before. Commits ------- 46c82d5 Mention lazy loading for Doctrine event listeners
2 parents 25224cc + 46c82d5 commit a64d407

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

doctrine/event_listeners_subscribers.rst

+53
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,58 @@ interface and have an event method for each event it subscribes to::
214214

215215
For a full reference, see chapter `The Event System`_ in the Doctrine documentation.
216216

217+
Lazy loading for Event Listeners
218+
--------------------------------
219+
220+
One subtle difference between listeners and subscribers is that Symfony can load
221+
entity listeners lazily. This means that your listener class will only be fetched
222+
from the service container (and thus be instantiated) once the event it is linked
223+
to actually fires.
224+
225+
Lazy loading might give you a slight performance improvement when your listener
226+
runs for events that rarely fire. Also, it can help you when you run into
227+
*circular dependency issues* that may occur when your listener service in turn
228+
depends on the DBAL connection.
229+
230+
To mark a listener service as lazily loaded, just add the ``lazy`` attribute
231+
to the tag like so:
232+
233+
.. configuration-block::
234+
235+
.. code-block:: yaml
236+
237+
services:
238+
my.listener:
239+
class: AppBundle\EventListener\SearchIndexer
240+
tags:
241+
- { name: doctrine.event_listener, event: postPersist, lazy: true }
242+
243+
.. code-block:: xml
244+
245+
<?xml version="1.0" ?>
246+
<container xmlns="http://symfony.com/schema/dic/services"
247+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">
248+
249+
<services>
250+
<service id="my.listener" class="AppBundle\EventListener\SearchIndexer">
251+
<tag name="doctrine.event_listener" event="postPersist" lazy="true" />
252+
</service>
253+
</services>
254+
</container>
255+
256+
.. code-block:: php
257+
258+
use AppBundle\EventListener\SearchIndexer;
259+
260+
$container
261+
->register('my.listener', SearchIndexer::class)
262+
->addTag('doctrine.event_listener', array('event' => 'postPersist', 'lazy' => 'true'))
263+
;
264+
265+
.. note::
266+
267+
  Marking an event listener as ``lazy`` has nothing to do with lazy service
268+
definitions which are described :doc:`in their own section </service_container/lazy_services>`
269+
217270
.. _`The Event System`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
218271
.. _`the Doctrine Documentation`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners

0 commit comments

Comments
 (0)