https://github.com/w3c/w3clifecycleeventsbundle
A Symfony bundle to dispatch usable entity lifecycle events (create, update, delete)
https://github.com/w3c/w3clifecycleeventsbundle
doctrine doctrine-orm events lifecycle lifecycle-events symfony-bundle symfony3
Last synced: 9 months ago
JSON representation
A Symfony bundle to dispatch usable entity lifecycle events (create, update, delete)
- Host: GitHub
- URL: https://github.com/w3c/w3clifecycleeventsbundle
- Owner: w3c
- License: other
- Created: 2017-02-28T14:12:23.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2025-01-10T15:10:17.000Z (about 1 year ago)
- Last Synced: 2025-04-11T18:00:06.830Z (9 months ago)
- Topics: doctrine, doctrine-orm, events, lifecycle, lifecycle-events, symfony-bundle, symfony3
- Language: PHP
- Homepage:
- Size: 190 KB
- Stars: 18
- Watchers: 10
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://github.com/w3c/W3CLifecycleEventsBundle/actions/workflows/symfony.yml)
[](https://coveralls.io/github/w3c/W3CLifecycleEventsBundle?branch=master)
[](https://insight.symfony.com/account/widget?project=b0d1493c-6de8-4c18-87ad-a12d2487fd59)
lifecycle-events-bundle
=======================
This Symfony bundle is meant to capture and dispatch events that happen throughout the lifecycle of entities:
- creation
- deletion
- updates
Doctrine already provides such events, but using them directly has a few shortcomings:
- You don't decide at which point in a action you want to dispatch events. Events are fired during a flush.
- When Doctrine events are fired, you are not assured that the entities have actually been saved in the database.
This is obvious for preUpdate (sent before persisting the changes), but postPersist and preRemove have the same issue:
if you persist two new entities in a single transaction, the first insert could work (thus an event would be sent) but
not the second, resulting in no entities being saved at all
This bundle aims at circumventing these issues by providing means to fire entity creation, deletion and update events
after a successful flush or whenever needed.
It also provides a set of attributes to configure what events should be sent and when.
This bundle was partially inspired by @kriswallsmith's talk
["Matters of State"](https://www.youtube.com/watch?v=lEiwP4w6mf4).
Installation
------------
Simply run assuming you have installed composer.phar or composer binary:
``` bash
$ php composer.phar require w3c/lifecycle-events-bundle 1.0.*
```
Finally, enable the bundle in the kernel:
``` php
setAutoDispatch(false);
[...]
}
```
Events can then be dispatched manually using the following:
``` php
dispatchEvents(); // manually dispatch all events
```
Special case: inheritance
-------------------------
If you use inheritance in your entities, make sure to set fields of the parent class(es) protected (or public) so that
changes to those can be monitored as belonging to subclasses.
Failing to do so may lead to `\ReflectionException` exceptions such as:
```
Property W3CGroup::$updated does not exist. Could this be a private field of a parent class?
```
Even if those fields are not monitored!