@@ -153,6 +153,7 @@ with the ``doctrine.event_listener`` tag:
153
153
154
154
.. code-block :: yaml
155
155
156
+ # config/services.yaml
156
157
services :
157
158
# ...
158
159
@@ -172,6 +173,7 @@ with the ``doctrine.event_listener`` tag:
172
173
173
174
.. code-block :: xml
174
175
176
+ <!-- config/services.xml -->
175
177
<?xml version =" 1.0" ?>
176
178
<container xmlns =" http://symfony.com/schema/dic/services"
177
179
xmlns : doctrine =" http://symfony.com/schema/dic/doctrine" >
@@ -195,6 +197,7 @@ with the ``doctrine.event_listener`` tag:
195
197
196
198
.. code-block :: php
197
199
200
+ // config/services.php
198
201
use App\EventListener\SearchIndexer;
199
202
200
203
// listeners are applied by default to all Doctrine connections
@@ -250,6 +253,7 @@ with the ``doctrine.orm.entity_listener`` tag:
250
253
251
254
.. code-block :: yaml
252
255
256
+ # config/services.yaml
253
257
services :
254
258
# ...
255
259
@@ -274,6 +278,7 @@ with the ``doctrine.orm.entity_listener`` tag:
274
278
275
279
.. code-block :: xml
276
280
281
+ <!-- config/services.xml -->
277
282
<?xml version =" 1.0" ?>
278
283
<container xmlns =" http://symfony.com/schema/dic/services"
279
284
xmlns : doctrine =" http://symfony.com/schema/dic/doctrine" >
@@ -302,6 +307,7 @@ with the ``doctrine.orm.entity_listener`` tag:
302
307
303
308
.. code-block :: php
304
309
310
+ // config/services.php
305
311
use App\Entity\User;
306
312
use App\EventListener\UserChangedNotifier;
307
313
@@ -330,7 +336,7 @@ Doctrine Lifecycle Subscribers
330
336
Lifecycle subscribers are defined as PHP classes that implement the
331
337
``Doctrine\Common\EventSubscriber `` interface and which listen to one or more
332
338
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
334
340
``postPersist ``, ``postRemove `` and ``postUpdate `` Doctrine events::
335
341
336
342
// src/EventListener/DatabaseActivitySubscriber.php
@@ -386,37 +392,70 @@ want to log all the database activity. To do so, define a listener for the
386
392
}
387
393
}
388
394
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:
396
437
397
438
.. configuration-block ::
398
439
399
440
.. code-block :: yaml
400
441
442
+ # config/services.yaml
401
443
services :
402
444
# ...
403
445
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
446
App\EventListener\DatabaseActivitySubscriber :
407
447
tags :
408
448
- { name: 'doctrine.event_subscriber', connection: 'default' }
409
449
410
450
.. code-block :: xml
411
451
452
+ <!-- config/services.xml -->
412
453
<?xml version =" 1.0" ?>
413
454
<container xmlns =" http://symfony.com/schema/dic/services"
414
455
xmlns : doctrine =" http://symfony.com/schema/dic/doctrine" >
415
456
<services >
416
457
<!-- ... -->
417
458
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
459
<service id =" App\EventListener\DatabaseActivitySubscriber" >
421
460
<tag name =" doctrine.event_subscriber" connection =" default" />
422
461
</service >
@@ -425,10 +464,9 @@ a service for it and :doc:`tag it </service_container/tags>` with the
425
464
426
465
.. code-block :: php
427
466
467
+ // config/services.php
428
468
use App\EventListener\DatabaseActivitySubscriber;
429
469
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
470
$container->autowire(DatabaseActivitySubscriber::class)
433
471
->addTag('doctrine.event_subscriber', ['connection' => 'default'])
434
472
;
0 commit comments