@@ -15,7 +15,7 @@ There are different ways to listen to these Doctrine events:
15
15
16
16
* **Lifecycle callbacks **, they are defined as public methods on the entity classes and
17
17
they are called when the events are triggered;
18
- * **Lifecycle listeners and subscribers **, they are classes with callback
18
+ * **Lifecycle listeners **, they are classes with callback
19
19
methods for one or more events and they are called for all entities;
20
20
* **Entity listeners **, they are similar to lifecycle listeners, but they are
21
21
called only for the entities of a certain class.
@@ -25,7 +25,7 @@ These are the **drawbacks and advantages** of each one:
25
25
* Callbacks have better performance because they only apply to a single entity
26
26
class, but you can't reuse the logic for different entities and they don't
27
27
have access to :doc: `Symfony services </service_container >`;
28
- * Lifecycle listeners and subscribers can reuse logic among different entities
28
+ * Lifecycle listeners can reuse logic among different entities
29
29
and can access Symfony services but their performance is worse because they
30
30
are called for all entities;
31
31
* Entity listeners have the same advantages of lifecycle listeners and they have
@@ -37,7 +37,7 @@ to learn everything about them.
37
37
38
38
.. seealso ::
39
39
40
- This article covers listeners and subscribers for Doctrine ORM. If you are
40
+ This article covers listeners for Doctrine ORM. If you are
41
41
using ODM for MongoDB, read the `DoctrineMongoDBBundle documentation `_.
42
42
43
43
Doctrine Lifecycle Callbacks
@@ -110,11 +110,6 @@ define a callback for the ``prePersist`` Doctrine event:
110
110
Doctrine Lifecycle Listeners
111
111
----------------------------
112
112
113
- .. deprecated :: 6.3
114
-
115
- Lifecycle subscribers are deprecated starting from Symfony 6.3 and will be
116
- removed in Symfony 7.0. Use lifecycle listeners instead.
117
-
118
113
Lifecycle listeners are defined as PHP classes that listen to a single Doctrine
119
114
event on all the application entities. For example, suppose that you want to
120
115
update some search index whenever a new entity is persisted in the database. To
@@ -201,7 +196,7 @@ listener in the Symfony application by creating a new service for it and
201
196
# this is the only required option for the lifecycle listener tag
202
197
event : ' postPersist'
203
198
204
- # listeners can define their priority in case multiple subscribers or listeners are associated
199
+ # listeners can define their priority in case listeners are associated
205
200
# to the same event (default priority = 0; higher numbers = listener is run earlier)
206
201
priority : 500
207
202
@@ -219,7 +214,7 @@ listener in the Symfony application by creating a new service for it and
219
214
220
215
<!--
221
216
* 'event' is the only required option that defines the lifecycle listener
222
- * 'priority': used when multiple subscribers or listeners are associated to the same event
217
+ * 'priority': used when multiple listeners are associated to the same event
223
218
* (default priority = 0; higher numbers = listener is run earlier)
224
219
* 'connection': restricts the listener to a specific Doctrine connection
225
220
-->
@@ -248,7 +243,7 @@ listener in the Symfony application by creating a new service for it and
248
243
// this is the only required option for the lifecycle listener tag
249
244
'event' => 'postPersist',
250
245
251
- // listeners can define their priority in case multiple subscribers or listeners are associated
246
+ // listeners can define their priority in case multiple listeners are associated
252
247
// to the same event (default priority = 0; higher numbers = listener is run earlier)
253
248
'priority' => 500,
254
249
@@ -262,12 +257,6 @@ listener in the Symfony application by creating a new service for it and
262
257
263
258
The `AsDoctrineListener `_ attribute was introduced in DoctrineBundle 2.7.2.
264
259
265
- .. tip ::
266
-
267
- Symfony loads (and instantiates) Doctrine listeners only when the related
268
- Doctrine event is actually fired; whereas Doctrine subscribers are always
269
- loaded (and instantiated) by Symfony, making them less performant.
270
-
271
260
.. tip ::
272
261
273
262
The value of the ``connection `` option can also be a
@@ -414,148 +403,6 @@ with the ``doctrine.orm.entity_listener`` tag as follows:
414
403
;
415
404
};
416
405
417
- Doctrine Lifecycle Subscribers
418
- ------------------------------
419
-
420
- Lifecycle subscribers are defined as PHP classes that implement the
421
- ``Doctrine\Common\EventSubscriber `` interface and which listen to one or more
422
- Doctrine events on all the application entities. For example, suppose that you
423
- want to log all the database activity. To do so, define a subscriber for the
424
- ``postPersist ``, ``postRemove `` and ``postUpdate `` Doctrine events::
425
-
426
- // src/EventListener/DatabaseActivitySubscriber.php
427
- namespace App\EventListener;
428
-
429
- use App\Entity\Product;
430
- use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
431
- use Doctrine\ORM\Event\PostPersistEventArgs;
432
- use Doctrine\ORM\Event\PostRemoveEventArgs;
433
- use Doctrine\ORM\Event\PostUpdateEventArgs;
434
- use Doctrine\ORM\Events;
435
-
436
- class DatabaseActivitySubscriber implements EventSubscriberInterface
437
- {
438
- // this method can only return the event names; you cannot define a
439
- // custom method name to execute when each event triggers
440
- public function getSubscribedEvents(): array
441
- {
442
- return [
443
- Events::postPersist,
444
- Events::postRemove,
445
- Events::postUpdate,
446
- ];
447
- }
448
-
449
- // callback methods must be called exactly like the events they listen to;
450
- // they receive an argument of type Post*EventArgs, which gives you access
451
- // to both the entity object of the event and the entity manager itself
452
- public function postPersist(PostPersistEventArgs $args): void
453
- {
454
- $this->logActivity('persist', $args->getObject());
455
- }
456
-
457
- public function postRemove(PostRemoveEventArgs $args): void
458
- {
459
- $this->logActivity('remove', $args->getObject());
460
- }
461
-
462
- public function postUpdate(PostUpdateEventArgs $args): void
463
- {
464
- $this->logActivity('update', $args->getObject());
465
- }
466
-
467
- private function logActivity(string $action, mixed $entity): void
468
- {
469
- // if this subscriber only applies to certain entity types,
470
- // add some code to check the entity type as early as possible
471
- if (!$entity instanceof Product) {
472
- return;
473
- }
474
-
475
- // ... get the entity information and log it somehow
476
- }
477
- }
478
-
479
- .. note ::
480
-
481
- In previous Doctrine versions, instead of ``Post*EventArgs `` classes, you had
482
- to use ``LifecycleEventArgs ``, which was deprecated in Doctrine ORM 2.14.
483
-
484
- If you're using the :ref: `default services.yaml configuration <service-container-services-load-example >`
485
- and DoctrineBundle 2.1 (released May 25, 2020) or newer, this example will already
486
- work! Otherwise, :ref: `create a service <service-container-creating-service >` for this
487
- subscriber and :doc: `tag it </service_container/tags >` with ``doctrine.event_subscriber ``.
488
-
489
- If you need to configure some option of the subscriber (e.g. its priority or
490
- Doctrine connection to use) you must do that in the manual service configuration:
491
-
492
- .. configuration-block ::
493
-
494
- .. code-block :: yaml
495
-
496
- # config/services.yaml
497
- services :
498
- # ...
499
-
500
- App\EventListener\DatabaseActivitySubscriber :
501
- tags :
502
- - name : ' doctrine.event_subscriber'
503
-
504
- # subscribers can define their priority in case multiple subscribers or listeners are associated
505
- # to the same event (default priority = 0; higher numbers = listener is run earlier)
506
- priority : 500
507
-
508
- # you can also restrict listeners to a specific Doctrine connection
509
- connection : ' default'
510
-
511
- .. code-block :: xml
512
-
513
- <!-- config/services.xml -->
514
- <?xml version =" 1.0" encoding =" UTF-8" ?>
515
- <container xmlns =" http://symfony.com/schema/dic/services"
516
- xmlns : doctrine =" http://symfony.com/schema/dic/doctrine" >
517
- <services >
518
- <!-- ... -->
519
-
520
- <!--
521
- * 'priority': used when multiple subscribers or listeners are associated to the same event
522
- * (default priority = 0; higher numbers = listener is run earlier)
523
- * 'connection': restricts the listener to a specific Doctrine connection
524
- -->
525
- <service id =" App\EventListener\DatabaseActivitySubscriber" >
526
- <tag name =" doctrine.event_subscriber" priority =" 500" connection =" default" />
527
- </service >
528
- </services >
529
- </container >
530
-
531
- .. code-block :: php
532
-
533
- // config/services.php
534
- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
535
-
536
- use App\EventListener\DatabaseActivitySubscriber;
537
-
538
- return static function (ContainerConfigurator $container): void {
539
- $services = $container->services();
540
-
541
- $services->set(DatabaseActivitySubscriber::class)
542
- ->tag('doctrine.event_subscriber'[
543
- // subscribers can define their priority in case multiple subscribers or listeners are associated
544
- // to the same event (default priority = 0; higher numbers = listener is run earlier)
545
- 'priority' => 500,
546
-
547
- // you can also restrict listeners to a specific Doctrine connection
548
- 'connection' => 'default',
549
- ])
550
- ;
551
- };
552
-
553
- .. tip ::
554
-
555
- Symfony loads (and instantiates) Doctrine subscribers whenever the
556
- application executes; whereas Doctrine listeners are only loaded when the
557
- related event is actually fired, making them more performant.
558
-
559
406
.. _`Doctrine` : https://www.doctrine-project.org/
560
407
.. _`lifecycle events` : https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/events.html#lifecycle-events
561
408
.. _`official docs about Doctrine events` : https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/events.html
0 commit comments