An open API service indexing awesome lists of open source software.

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)

Awesome Lists containing this project

README

          

[![Build Status](https://github.com/w3c/W3CLifecycleEventsBundle/actions/workflows/symfony.yml/badge.svg)](https://github.com/w3c/W3CLifecycleEventsBundle/actions/workflows/symfony.yml)
[![Coverage Status](https://coveralls.io/repos/github/w3c/W3CLifecycleEventsBundle/badge.svg?branch=master)](https://coveralls.io/github/w3c/W3CLifecycleEventsBundle?branch=master)
[![SensioLabsInsight](https://insight.symfony.com/projects/b0d1493c-6de8-4c18-87ad-a12d2487fd59/mini.svg)](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!