Skip to content

[DoctrineBridge] update doctrine event listeners doc for Symfony 4.2 change #9973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 29, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 11 additions & 51 deletions doctrine/event_listeners_subscribers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,57 +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.

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
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.

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.
That's why it is preferable to use entity listeners instead of subscribers
whenever possible.

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

<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">

<services>
<service id="App\EventListener\SearchIndexer" autowire="true">
<tag name="doctrine.event_listener" event="postPersist" lazy="true" />
</service>
</services>
</container>

.. 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 </service_container/lazy_services>`

.. _`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
.. versionadded:: 4.2
The default lazy behavior of Doctrine entity listeners was introduced in
Copy link
Contributor Author

@dmaicher dmaicher Jun 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically this is not correct. It was possible even before Symfony 4.2 but you had to manually add a lazy=true attribute for the tag. Now since 4.2 it will be always lazy 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry I missed the "default" while reading it. All good 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that phrase I tried to say: "in 4.2 the new thing is that is lazy by default". But it looks like I failed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmaicher in 4.2 they are lazy by default ... or are they mandatory lazy? In other words, can you say lazy:false in 4.2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No they are always lazy now. So mandatory.

Symfony 4.2.