Skip to content

Commit eb24823

Browse files
committed
minor #12482 [Doctrine] EventSubscriber is not autoconfigured (alanpoulain)
This PR was merged into the 4.3 branch. Discussion ---------- [Doctrine] EventSubscriber is not autoconfigured Correct me if I'm wrong, but it seems the `Doctrine\Common\EventSubscriber` is not autoconfigured. This PR removes the wrong affirmation. Commits ------- 752061d EventSubscriber is not autoconfigured
2 parents 7096d54 + 752061d commit eb24823

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

doctrine/events.rst

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Doctrine Lifecycle Subscribers
330330
Lifecycle subscribers are defined as PHP classes that implement the
331331
``Doctrine\Common\EventSubscriber`` interface and which listen to one or more
332332
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
333+
want to log all the database activity. To do so, define a subscriber for the
334334
``postPersist``, ``postRemove`` and ``postUpdate`` Doctrine events::
335335

336336
// src/EventListener/DatabaseActivitySubscriber.php
@@ -386,13 +386,45 @@ want to log all the database activity. To do so, define a listener for the
386386
}
387387
}
388388

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:
389+
The next step is to enable the Doctrine subscriber in the Symfony application by
390+
creating a new service for it and :doc:`tagging it </service_container/tags>`
391+
with the ``doctrine.event_subscriber`` tag:
392+
393+
.. configuration-block::
394+
395+
.. code-block:: yaml
396+
397+
services:
398+
# ...
399+
400+
App\EventListener\DatabaseActivitySubscriber:
401+
tags:
402+
- { name: 'doctrine.event_subscriber' }
403+
404+
.. code-block:: xml
405+
406+
<?xml version="1.0" ?>
407+
<container xmlns="http://symfony.com/schema/dic/services"
408+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">
409+
<services>
410+
<!-- ... -->
411+
412+
<service id="App\EventListener\DatabaseActivitySubscriber">
413+
<tag name="doctrine.event_subscriber"/>
414+
</service>
415+
</services>
416+
</container>
417+
418+
.. code-block:: php
419+
420+
use App\EventListener\DatabaseActivitySubscriber;
421+
422+
$container->autowire(DatabaseActivitySubscriber::class)
423+
->addTag('doctrine.event_subscriber')
424+
;
425+
426+
If you need to associate the subscriber with a specific Doctrine connection, you
427+
can do it in the service configuration:
396428

397429
.. configuration-block::
398430

@@ -401,8 +433,6 @@ a service for it and :doc:`tag it </service_container/tags>` with the
401433
services:
402434
# ...
403435
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)
406436
App\EventListener\DatabaseActivitySubscriber:
407437
tags:
408438
- { name: 'doctrine.event_subscriber', connection: 'default' }
@@ -415,8 +445,6 @@ a service for it and :doc:`tag it </service_container/tags>` with the
415445
<services>
416446
<!-- ... -->
417447
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) -->
420448
<service id="App\EventListener\DatabaseActivitySubscriber">
421449
<tag name="doctrine.event_subscriber" connection="default"/>
422450
</service>
@@ -427,8 +455,6 @@ a service for it and :doc:`tag it </service_container/tags>` with the
427455
428456
use App\EventListener\DatabaseActivitySubscriber;
429457
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)
432458
$container->autowire(DatabaseActivitySubscriber::class)
433459
->addTag('doctrine.event_subscriber', ['connection' => 'default'])
434460
;

0 commit comments

Comments
 (0)