@@ -330,7 +330,7 @@ Doctrine Lifecycle Subscribers
330
330
Lifecycle subscribers are defined as PHP classes that implement the
331
331
``Doctrine\Common\EventSubscriber `` interface and which listen to one or more
332
332
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
334
334
``postPersist ``, ``postRemove `` and ``postUpdate `` Doctrine events::
335
335
336
336
// src/EventListener/DatabaseActivitySubscriber.php
@@ -386,13 +386,45 @@ want to log all the database activity. To do so, define a listener for the
386
386
}
387
387
}
388
388
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:
396
428
397
429
.. configuration-block ::
398
430
@@ -401,8 +433,6 @@ a service for it and :doc:`tag it </service_container/tags>` with the
401
433
services :
402
434
# ...
403
435
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)
406
436
App\EventListener\DatabaseActivitySubscriber :
407
437
tags :
408
438
- { name: 'doctrine.event_subscriber', connection: 'default' }
@@ -415,8 +445,6 @@ a service for it and :doc:`tag it </service_container/tags>` with the
415
445
<services >
416
446
<!-- ... -->
417
447
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) -->
420
448
<service id =" App\EventListener\DatabaseActivitySubscriber" >
421
449
<tag name =" doctrine.event_subscriber" connection =" default" />
422
450
</service >
@@ -427,8 +455,6 @@ a service for it and :doc:`tag it </service_container/tags>` with the
427
455
428
456
use App\EventListener\DatabaseActivitySubscriber;
429
457
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)
432
458
$container->autowire(DatabaseActivitySubscriber::class)
433
459
->addTag('doctrine.event_subscriber', ['connection' => 'default'])
434
460
;
0 commit comments