Skip to content

Commit 12c6b20

Browse files
committed
Merge branch '4.4'
* 4.4: Added the missing file paths of config files EventSubscriber is not autoconfigured
2 parents 99371e3 + 361fbab commit 12c6b20

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

doctrine/events.rst

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ with the ``doctrine.event_listener`` tag:
153153

154154
.. code-block:: yaml
155155
156+
# config/services.yaml
156157
services:
157158
# ...
158159
@@ -172,6 +173,7 @@ with the ``doctrine.event_listener`` tag:
172173
173174
.. code-block:: xml
174175
176+
<!-- config/services.xml -->
175177
<?xml version="1.0" ?>
176178
<container xmlns="http://symfony.com/schema/dic/services"
177179
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">
@@ -195,6 +197,7 @@ with the ``doctrine.event_listener`` tag:
195197
196198
.. code-block:: php
197199
200+
// config/services.php
198201
use App\EventListener\SearchIndexer;
199202
200203
// listeners are applied by default to all Doctrine connections
@@ -250,6 +253,7 @@ with the ``doctrine.orm.entity_listener`` tag:
250253

251254
.. code-block:: yaml
252255
256+
# config/services.yaml
253257
services:
254258
# ...
255259
@@ -274,6 +278,7 @@ with the ``doctrine.orm.entity_listener`` tag:
274278
275279
.. code-block:: xml
276280
281+
<!-- config/services.xml -->
277282
<?xml version="1.0" ?>
278283
<container xmlns="http://symfony.com/schema/dic/services"
279284
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">
@@ -302,6 +307,7 @@ with the ``doctrine.orm.entity_listener`` tag:
302307
303308
.. code-block:: php
304309
310+
// config/services.php
305311
use App\Entity\User;
306312
use App\EventListener\UserChangedNotifier;
307313
@@ -330,7 +336,7 @@ Doctrine Lifecycle Subscribers
330336
Lifecycle subscribers are defined as PHP classes that implement the
331337
``Doctrine\Common\EventSubscriber`` interface and which listen to one or more
332338
Doctrine events on all the application entities. For example, suppose that you
333-
want to log all the database activity. To do so, define a listener for the
339+
want to log all the database activity. To do so, define a subscriber for the
334340
``postPersist``, ``postRemove`` and ``postUpdate`` Doctrine events::
335341

336342
// src/EventListener/DatabaseActivitySubscriber.php
@@ -386,37 +392,70 @@ want to log all the database activity. To do so, define a listener for the
386392
}
387393
}
388394

389-
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
390-
Symfony will register the Doctrine subscriber automatically thanks to the
391-
:ref:`autoconfigure <services-autoconfigure>` and
392-
:doc:`autowiring </service_container/autowiring>` features. However, if you need
393-
to associate the subscriber with a specific Doctrine connection, you must define
394-
a service for it and :doc:`tag it </service_container/tags>` with the
395-
``doctrine.event_subscriber`` tag:
395+
The next step is to enable the Doctrine subscriber in the Symfony application by
396+
creating a new service for it and :doc:`tagging it </service_container/tags>`
397+
with the ``doctrine.event_subscriber`` tag:
398+
399+
.. configuration-block::
400+
401+
.. code-block:: yaml
402+
403+
# config/services.yaml
404+
services:
405+
# ...
406+
407+
App\EventListener\DatabaseActivitySubscriber:
408+
tags:
409+
- { name: 'doctrine.event_subscriber' }
410+
411+
.. code-block:: xml
412+
413+
<!-- config/services.xml -->
414+
<?xml version="1.0" ?>
415+
<container xmlns="http://symfony.com/schema/dic/services"
416+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">
417+
<services>
418+
<!-- ... -->
419+
420+
<service id="App\EventListener\DatabaseActivitySubscriber">
421+
<tag name="doctrine.event_subscriber"/>
422+
</service>
423+
</services>
424+
</container>
425+
426+
.. code-block:: php
427+
428+
// config/services.php
429+
use App\EventListener\DatabaseActivitySubscriber;
430+
431+
$container->autowire(DatabaseActivitySubscriber::class)
432+
->addTag('doctrine.event_subscriber')
433+
;
434+
435+
If you need to associate the subscriber with a specific Doctrine connection, you
436+
can do it in the service configuration:
396437

397438
.. configuration-block::
398439

399440
.. code-block:: yaml
400441
442+
# config/services.yaml
401443
services:
402444
# ...
403445
404-
# in most applications you don't need to define a service for your
405-
# subscriber (this is only needed when using a custom Doctrine connection)
406446
App\EventListener\DatabaseActivitySubscriber:
407447
tags:
408448
- { name: 'doctrine.event_subscriber', connection: 'default' }
409449
410450
.. code-block:: xml
411451
452+
<!-- config/services.xml -->
412453
<?xml version="1.0" ?>
413454
<container xmlns="http://symfony.com/schema/dic/services"
414455
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">
415456
<services>
416457
<!-- ... -->
417458
418-
<!-- in most applications you don't need to define a service for your
419-
subscriber (this is only needed when using a custom Doctrine connection) -->
420459
<service id="App\EventListener\DatabaseActivitySubscriber">
421460
<tag name="doctrine.event_subscriber" connection="default"/>
422461
</service>
@@ -425,10 +464,9 @@ a service for it and :doc:`tag it </service_container/tags>` with the
425464
426465
.. code-block:: php
427466
467+
// config/services.php
428468
use App\EventListener\DatabaseActivitySubscriber;
429469
430-
// in most applications you don't need to define a service for your
431-
// subscriber (this is only needed when using a custom Doctrine connection)
432470
$container->autowire(DatabaseActivitySubscriber::class)
433471
->addTag('doctrine.event_subscriber', ['connection' => 'default'])
434472
;

0 commit comments

Comments
 (0)