@@ -198,22 +198,28 @@ with the ``doctrine.event_listener`` tag:
198
198
.. code-block :: php
199
199
200
200
// config/services.php
201
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
202
+
201
203
use App\EventListener\SearchIndexer;
202
204
203
- // listeners are applied by default to all Doctrine connections
204
- $container->autowire(SearchIndexer::class)
205
- ->addTag('doctrine.event_listener', [
206
- // this is the only required option for the lifecycle listener tag
207
- 'event' => 'postPersist',
205
+ return static function (ContainerConfigurator $container) {
206
+ $services = $configurator->services();
207
+
208
+ // listeners are applied by default to all Doctrine connections
209
+ $services->set(SearchIndexer::class)
210
+ ->tag('doctrine.event_listener', [
211
+ // this is the only required option for the lifecycle listener tag
212
+ 'event' => 'postPersist',
208
213
209
- // listeners can define their priority in case multiple listeners are associated
210
- // to the same event (default priority = 0; higher numbers = listener is run earlier)
211
- 'priority' => 500,
214
+ // listeners can define their priority in case multiple listeners are associated
215
+ // to the same event (default priority = 0; higher numbers = listener is run earlier)
216
+ 'priority' => 500,
212
217
213
- # you can also restrict listeners to a specific Doctrine connection
214
- 'connection' => 'default',
215
- ])
216
- ;
218
+ # you can also restrict listeners to a specific Doctrine connection
219
+ 'connection' => 'default',
220
+ ])
221
+ ;
222
+ };
217
223
218
224
.. tip ::
219
225
@@ -314,29 +320,35 @@ with the ``doctrine.orm.entity_listener`` tag:
314
320
.. code-block :: php
315
321
316
322
// config/services.php
323
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
324
+
317
325
use App\Entity\User;
318
326
use App\EventListener\UserChangedNotifier;
319
327
320
- $container->autowire(UserChangedNotifier::class)
321
- ->addTag('doctrine.orm.entity_listener', [
322
- // These are the options required to define the entity listener:
323
- 'event' => 'postUpdate',
324
- 'entity' => User::class,
328
+ return static function (ContainerConfigurator $container) {
329
+ $services = $configurator->services();
330
+
331
+ $services->set(UserChangedNotifier::class)
332
+ ->tag('doctrine.orm.entity_listener', [
333
+ // These are the options required to define the entity listener:
334
+ 'event' => 'postUpdate',
335
+ 'entity' => User::class,
325
336
326
- // These are other options that you may define if needed:
337
+ // These are other options that you may define if needed:
327
338
328
- // set the 'lazy' option to TRUE to only instantiate listeners when they are used
329
- // 'lazy' => true,
339
+ // set the 'lazy' option to TRUE to only instantiate listeners when they are used
340
+ // 'lazy' => true,
330
341
331
- // set the 'entity_manager' option if the listener is not associated to the default manager
332
- // 'entity_manager' => 'custom',
342
+ // set the 'entity_manager' option if the listener is not associated to the default manager
343
+ // 'entity_manager' => 'custom',
333
344
334
- // by default, Symfony looks for a method called after the event (e.g. postUpdate())
335
- // if it doesn't exist, it tries to execute the '__invoke()' method, but you can
336
- // configure a custom method name with the 'method' option
337
- // 'method' => 'checkUserChanges',
338
- ])
339
- ;
345
+ // by default, Symfony looks for a method called after the event (e.g. postUpdate())
346
+ // if it doesn't exist, it tries to execute the '__invoke()' method, but you can
347
+ // configure a custom method name with the 'method' option
348
+ // 'method' => 'checkUserChanges',
349
+ ])
350
+ ;
351
+ };
340
352
341
353
Doctrine Lifecycle Subscribers
342
354
------------------------------
@@ -434,11 +446,17 @@ with the ``doctrine.event_subscriber`` tag:
434
446
.. code-block :: php
435
447
436
448
// config/services.php
449
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
450
+
437
451
use App\EventListener\DatabaseActivitySubscriber;
438
452
439
- $container->autowire(DatabaseActivitySubscriber::class)
440
- ->addTag('doctrine.event_subscriber')
441
- ;
453
+ return static function (ContainerConfigurator $container) {
454
+ $services = $configurator->services();
455
+
456
+ $services->set(DatabaseActivitySubscriber::class)
457
+ ->tag('doctrine.event_subscriber')
458
+ ;
459
+ };
442
460
443
461
If you need to associate the subscriber with a specific Doctrine connection, you
444
462
can do it in the service configuration:
@@ -473,11 +491,17 @@ can do it in the service configuration:
473
491
.. code-block :: php
474
492
475
493
// config/services.php
494
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
495
+
476
496
use App\EventListener\DatabaseActivitySubscriber;
477
497
478
- $container->autowire(DatabaseActivitySubscriber::class)
479
- ->addTag('doctrine.event_subscriber', ['connection' => 'default'])
480
- ;
498
+ return static function (ContainerConfigurator $container) {
499
+ $services = $configurator->services();
500
+
501
+ $services->set(DatabaseActivitySubscriber::class)
502
+ ->tag('doctrine.event_subscriber', ['connection' => 'default'])
503
+ ;
504
+ };
481
505
482
506
.. tip ::
483
507
0 commit comments