Skip to content

Commit b8a7564

Browse files
committed
minor #9973 [DoctrineBridge] update doctrine event listeners doc for Symfony 4.2 change (dmaicher, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- [DoctrineBridge] update doctrine event listeners doc for Symfony 4.2 change See symfony/symfony#27675. Doctrine entity listeners are always lazily instantiated as of Symfony 4.2. Commits ------- 4409fd6 Explain that lazy listeners are mandatory, not default b56ef9e Update event_listeners_subscribers.rst d8bb849 Reworded and added the versionadded directive 601450e Update event_listeners_subscribers.rst
2 parents f5e8ac9 + 4409fd6 commit b8a7564

File tree

1 file changed

+11
-51
lines changed

1 file changed

+11
-51
lines changed

doctrine/event_listeners_subscribers.rst

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -186,57 +186,17 @@ interface and have an event method for each event it subscribes to::
186186

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

189-
Lazy loading for Event Listeners
190-
--------------------------------
189+
Performance Considerations
190+
--------------------------
191191

192-
One subtle difference between listeners and subscribers is that Symfony can load
193-
entity listeners lazily. This means that your listener class will only be fetched
194-
from the service container (and thus be instantiated) once the event it is linked
195-
to actually fires.
192+
One important difference between listeners and subscribers is that Symfony loads
193+
entity listeners lazily. This means that the listener classes are only fetched
194+
from the service container (and instantiated) if the related event is actually
195+
fired.
196196

197-
Lazy loading might give you a slight performance improvement when your listener
198-
runs for events that rarely fire. Also, it can help you when you run into
199-
*circular dependency issues* that may occur when your listener service in turn
200-
depends on the DBAL connection.
197+
That's why it is preferable to use entity listeners instead of subscribers
198+
whenever possible.
201199

202-
To mark a listener service as lazily loaded, just add the ``lazy`` attribute
203-
to the tag like so:
204-
205-
.. configuration-block::
206-
207-
.. code-block:: yaml
208-
209-
services:
210-
App\EventListener\SearchIndexer:
211-
tags:
212-
- { name: doctrine.event_listener, event: postPersist, lazy: true }
213-
214-
.. code-block:: xml
215-
216-
<?xml version="1.0" ?>
217-
<container xmlns="http://symfony.com/schema/dic/services"
218-
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">
219-
220-
<services>
221-
<service id="App\EventListener\SearchIndexer" autowire="true">
222-
<tag name="doctrine.event_listener" event="postPersist" lazy="true" />
223-
</service>
224-
</services>
225-
</container>
226-
227-
.. code-block:: php
228-
229-
use App\EventListener\SearchIndexer;
230-
231-
$container
232-
->autowire(SearchIndexer::class)
233-
->addTag('doctrine.event_listener', array('event' => 'postPersist', 'lazy' => 'true'))
234-
;
235-
236-
.. note::
237-
238-
  Marking an event listener as ``lazy`` has nothing to do with lazy service
239-
definitions which are described :doc:`in their own section </service_container/lazy_services>`
240-
241-
.. _`The Event System`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
242-
.. _`the Doctrine Documentation`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners
200+
.. versionadded:: 4.2
201+
Starting from Symfony 4.2, Doctrine entity listeners are always lazy. In
202+
previous Symfony versions this behavior was configurable.

0 commit comments

Comments
 (0)