PHPackages                             vanio/doctrine-domain-events - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. vanio/doctrine-domain-events

ActiveLibrary[Database &amp; ORM](/categories/database)

vanio/doctrine-domain-events
============================

An extension for Doctrine2 ORM allowing usage of domain events and an ability to flush inside it's listeners.

720.3k↓35.5%11PHPCI failing

Since Aug 24Pushed 10mo ago5 watchersCompare

[ Source](https://github.com/vaniocz/doctrine-domain-events)[ Packagist](https://packagist.org/packages/vanio/doctrine-domain-events)[ RSS](/packages/vanio-doctrine-domain-events/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (1)

Doctrine Domain Events
======================

[](#doctrine-domain-events)

[![Build Status](https://camo.githubusercontent.com/b653fdd685ca50b934c151b220c863cffe36c46f571fea6e1d296b04d908895e/68747470733a2f2f7472617669732d63692e6f72672f76616e696f637a2f646f637472696e652d646f6d61696e2d6576656e74732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/vaniocz/doctrine-domain-events)[![Coverage Status](https://camo.githubusercontent.com/b098cdf09dbcfc25108f4ad6b2b03ef3deb77d208982938339e329391e072f6b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f76616e696f637a2f646f637472696e652d646f6d61696e2d6576656e74732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/vaniocz/doctrine-domain-events?branch=master)[![PHP7](https://camo.githubusercontent.com/1707d1cf35d1b3dba207caa68b99d5e4af7689d1c4bf5ad784bf4b1ec532f507/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372d3642374542392e737667)](https://camo.githubusercontent.com/1707d1cf35d1b3dba207caa68b99d5e4af7689d1c4bf5ad784bf4b1ec532f507/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372d3642374542392e737667)[![License](https://camo.githubusercontent.com/88e50fa5f571f989d53c551f164a75ed84485caae339d3e368fb624a7a526027/68747470733a2f2f706f7365722e707567782e6f72672f76616e696f2f646f637472696e652d646f6d61696e2d6576656e74732f6c6963656e7365)](https://github.com/vaniocz/doctrine-domain-events/blob/master/LICENSE)

Very lightweight extension for Doctrine2 ORM allowing usage of domain events together with an ability to flush inside it's listeners. This library is inspired in Beberlei's [Doctrine and Domain Events article](http://www.whitewashing.de/2013/07/24/doctrine_and_domainevents.html).

Installation
============

[](#installation)

Installation can be done as usually using composer. `composer require vanio/doctrine-domain-events`

You also need to register an instance of `Vanio\DoctrineDomainEvents\DoctrineDomainEventDispatcher` inside Doctrine's event manager.

```
use Vanio\DoctrineDomainEvents\DoctrineDomainEventDispatcher;

$eventManager->addSubscriber(new DoctrineDomainEventDispatcher);
```

Usage
=====

[](#usage)

Setting up your entity
----------------------

[](#setting-up-your-entity)

First make sure your entity implements `Vanio\DoctrineDomainEvent` interface and uses the `Vanio\DoctrineDomainEvent\EventProviderTrait`.

```
use Vanio\DoctrineDomainEvent\EventProvider;
use Vanio\DoctrineDomainEvent\EventProviderTrait;

class MyEntity implements EventProvider
{
    use EventProviderTrait;
}
```

Raising domain events
---------------------

[](#raising-domain-events)

To raise a domain event from your entity use the `raise` method. This method expects either an instance of `Vanio\DoctrineDomainEvent\DomainEvent` or just a string representing the domain event name (and optional array of properties). In case of passing a string it will be turned into an instance of the `DomainEvent` class. If you prefer to create your own event classes you can do so by extending the `DomainEvent` class, just don't forget to extend the `name`method. The event is dispatched at the end of transaction once your entity has been flushed and all the changes are projected into database so it is possible to both perform database queries over the changes as well as cancel the transaction. Event names will be used as method names of corresponding listeners because this implementation uses the built-in Doctrine event manager to keep it simple thus you need to make sure all your **event names are globally unique**.

```
use Vanio\DoctrineDomainEvent\EventProvider;
use Vanio\DoctrineDomainEvent\EventProviderTrait;

class Article implements EventProvider
{
    use EventProviderTrait;

    const EVENT_ARTICLE_PUBLISHED = 'onArticlePublish';

    // ...

    public function publish()
    {
        $this->raise(self::EVENT_ARTICLE_PUBLISHED, ['property' => 'value']);
    }
}
```

Listening to domain events
--------------------------

[](#listening-to-domain-events)

Listening to domain events is done the same way as listening to standard Doctrine events because it uses the same event manager. You need to either prepare a listener and register it to appropriate domain event yourself or you can use a subscriber that know which events it listens to. More information about the event system can be found inside [Doctrine documentation](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html).

### Listening to domain events using a listener

[](#listening-to-domain-events-using-a-listener)

```
class Listener
{
    public function onArticlePublish(DomainEvent $event)
    {
        // ...
    }
}

$eventManager->addListener(Article::EVENT_ARTICLE_PUBLISHED, new Listener);
```

### Listening to domain events using a subscriber

[](#listening-to-domain-events-using-a-subscriber)

```
use Doctrine\Common\EventSubscriber;

class Subscriber implements EventSubscriber
{
    public function onArticlePublish(DomainEvent $event)
    {
        // ...
    }

    public function getSubscribedEvents(): array
    {
        return [Article::EVENT_ARTICLE_PUBLISHED];
    }
}

$eventManager->addSubscriber(new Subscriber);
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 Bus Factor1

Top contributor holds 88.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/a0d882c3421e9c9188486977fb9193ee5840db4e1ac071fae1305112c92ab04a?d=identicon)[maryo](/maintainers/maryo)

![](https://www.gravatar.com/avatar/83fd24874418acb2e635bc408732703403e16fea6c003224f1e066f07e239bcb?d=identicon)[stanleyk](/maintainers/stanleyk)

---

Top Contributors

[![maryo](https://avatars.githubusercontent.com/u/900769?v=4)](https://github.com/maryo "maryo (8 commits)")[![nixbody](https://avatars.githubusercontent.com/u/1381401?v=4)](https://github.com/nixbody "nixbody (1 commits)")

### Embed Badge

![Health badge](/badges/vanio-doctrine-domain-events/health.svg)

```
[![Health](https://phpackages.com/badges/vanio-doctrine-domain-events/health.svg)](https://phpackages.com/packages/vanio-doctrine-domain-events)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
