PHPackages                             dsantang/domain-events-doctrine - 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. dsantang/domain-events-doctrine

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

dsantang/domain-events-doctrine
===============================

Package that provides an integration with Doctrine ORM to automatically dispatch Domain Events.

v0.10.0(3y ago)223.9k[1 PRs](https://github.com/dsantang/domain-events-doctrine/pulls)MITPHPPHP ^8.1

Since Nov 9Pushed 1y ago2 watchersCompare

[ Source](https://github.com/dsantang/domain-events-doctrine)[ Packagist](https://packagist.org/packages/dsantang/domain-events-doctrine)[ RSS](/packages/dsantang-domain-events-doctrine/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (15)Versions (16)Used By (0)

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

[](#domain-events-doctrine)

[![CircleCI](https://camo.githubusercontent.com/4f528399a79058e608bdd13e127c440251f53562110308803486a9e2ea6169d3/68747470733a2f2f636972636c6563692e636f6d2f67682f6473616e74616e672f646f6d61696e2d6576656e74732d646f637472696e652e7376673f7374796c653d737667)](https://circleci.com/gh/dsantang/domain-events-doctrine)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/32d06c2bde43653b28669b1f91a0d68b80e0e65a14f275cf50893598066b6bd9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6473616e74616e672f646f6d61696e2d6576656e74732d646f637472696e652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/dsantang/domain-events-doctrine/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/1258cf926579de5ef7a6d4938aefc31d8346bb51c9d77da611235ae5b3691ed0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6473616e74616e672f646f6d61696e2d6576656e74732d646f637472696e652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/dsantang/domain-events-doctrine/?branch=master)[![Build Status](https://camo.githubusercontent.com/19756568f708264eb3b33431d8cece4d72ec75ab1d802e2725e1fff16a8739fd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6473616e74616e672f646f6d61696e2d6576656e74732d646f637472696e652f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/dsantang/domain-events-doctrine/build-status/master)[![SymfonyInsight](https://camo.githubusercontent.com/dde3105114cdbdd110fc4c5b3a81e78564d3395dedb0bae898cecca5cb468f57/68747470733a2f2f696e73696768742e73796d666f6e792e636f6d2f70726f6a656374732f31616430656463332d306639382d343761662d383965332d3433343632386531666337612f736d616c6c2e737667)](https://insight.symfony.com/projects/1ad0edc3-0f98-47af-89e3-434628e1fc7a)

This package is meant to be used in conjunction with [dsantang/domain-events](https://github.com/dsantang/domain-events),
which provides the basic building blocks needed in order to create your application's domain events.
**This package provides an integration with Doctrine ORM**,
in order to automatically dispatch the recorded domain events once the ORM's `flush()` operation is successful.
Events are dispatched via a Symfony's `EventDispatcherInterface`.

Installation
------------

[](#installation)

The suggested installation method is via [composer](https://getcomposer.org/):

```
php composer.phar require dsantang/domain-events-doctrine
```

Configuration
-------------

[](#configuration)

In order to automagically dispatch your domain events, you first need to add two event listeners to your Doctrine's `EventManager`. These Doctrine's listeners will be listening to Doctrine's lifecycle events, and, only once the transaction has successfully been made, they'll dispatch your domain events.

```
use Dsantang\DomainEventsDoctrine\EventsRecorder\DoctrineEventsRecorder;
use Dsantang\DomainEventsDoctrine\Dispatcher\DoctrineEventsDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Doctrine\Common\EventManager;

// ...

$evm = new EventManager();

$dispatcher = new EventDispatcher();

// You can register here your domain event listeners to your dispatcher...

$tracker = new EventsTracker();

// Make sure they share the same `EventsTracker` instance.
$evm->addEventListener([Events::onFlush], new DoctrineEventsRecorder($tracker));
$evm->addEventListener([Events::postFlush], new DoctrineEventsDispatcher($tracker, $dispatcher));

// Remember to correctly wire this EventManager to your ORM.
```

That's it! You're all set! Now you can add as many Symfony's listeners as you need to your `$dispatcher`,
and you'll be able to react to the domain events raised by your application.

Outbox pattern
--------------

[](#outbox-pattern)

This library also provides support for the [Outbox pattern](https://microservices.io/patterns/data/application-events.html) implementation.
The idea behind the implementation is to be able to add entities to an "ongoing" transaction by hooking into Doctrine's `onFlush` event, creating "outbox" entries based on the application's domain events, and safely store them using the same DB transaction.

In order to enrich your Domain Event and be able to set all the data required by the Outbox entity, you need to create a `Converter` class (by implementing `Dsantang\DomainEventsDoctrine\Outbox\Converter` interface) An example of an outbox event is as follows:

```
use Dsantang\DomainEvents\DomainEvent;
use Dsantang\DomainEventsDoctrine\Outbox\Converter;
use Dsantang\DomainEventsDoctrine\Outbox\OutboxEntry;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

final class YourOutboxConverter implements Converter
{
    public function convert(DomainEvent $domainEvent) : OutboxEntry
    {
        return new YourOutboxEntry($domainEvent);
    }
}

final class YourOutboxEntry implements OutboxEntry
{
    /** @var DomainEvent */
    private $domainEvent;

    public function __construct(DomainEvent $domainEvent)
    {
        $this->domainEvent = $domainEvent;
    }

    public function getName() : string
    {
        return 'OrderDispatched';
    }

    public function getAggregateId() : UuidInterface
    {
        return Uuid::fromString('d1702762-548b-11e9-8647-d663bd873d93');
    }

    public function getAggregateType() : string
    {
        return 'Order';
    }

    public function getPayloadType() : string
    {
        return 'OrderStructure';
    }

    public function getMessageKey() : string
    {
        return 'd663bd873d93';
    }

    public function getMessageRoute() : string
    {
        return 'aggregate.order';
    }

    public function getMessageType() : string
    {
        return 'OrderCreated';
    }

    public function getPayload() : string
    {
        return json_encode($this->domainEvent)
    }

    public function getSchemaVersion() : int
    {
        return 1;
    }
}
```

In order to persist your Outbox entries, you must create a Doctrine Entity class inside your application that extends `Dsantang\DomainEventsDoctrine\Outbox\OutboxMappedSuperclass`.
Please note that this approach uses [Doctrine's Inheritance Mapping](https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html#mapped-superclasses):

Here is an example of an Outbox Entity class:

```
namespace YourNamespace;

use Doctrine\ORM\Mapping as ORM;
use Dsantang\DomainEventsDoctrine\Outbox\OutboxMappedSuperclass;

/**
 * @ORM\Entity()
 * @ORM\Table
 */
class YourOutboxEntity extends OutboxMappedSuperclass
{
    /**
     * @ORM\Column(type="string")
     *
     * @var string
     */
    private $someAdditionalField;
}
```

And an example of the required configuration as is follows:
**Warning:** this solution assumes that you're using `Dsantang\DomainEvents\DomainEvent` in order to raise your domain events.

```
use Dsantang\DomainEventsDoctrine\Outbox\MapBased;
use Dsantang\DomainEventsDoctrine\Outbox\OutboxMappedSuperclass;

// Your class must extend OutboxMappedSuperclass
$yourOutboxEntity = new YourOutboxEntity();

$mapBased = new MapBased($yourOutboxEntity);
$mapBased->addConverter('YouNamespace\YourDomainEvent', new YourOutboxConverter());

// Always use with OnFlush event
$evm->addEventListener([Events::onFlush], $mapBased);
```

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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.

###  Release Activity

Cadence

Every ~116 days

Recently: every ~310 days

Total

14

Last Release

1224d ago

PHP version history (3 changes)v0.1.0PHP ^7.2

v0.8.0PHP ^7.4 || ^8.0

v0.10.0PHP ^8.1

### Community

Maintainers

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

---

Top Contributors

[![gPinato](https://avatars.githubusercontent.com/u/6040180?v=4)](https://github.com/gPinato "gPinato (24 commits)")[![dsantang](https://avatars.githubusercontent.com/u/6401532?v=4)](https://github.com/dsantang "dsantang (21 commits)")[![fabiotc](https://avatars.githubusercontent.com/u/70739?v=4)](https://github.com/fabiotc "fabiotc (9 commits)")[![gfornari](https://avatars.githubusercontent.com/u/5920084?v=4)](https://github.com/gfornari "gfornari (4 commits)")[![agustingomes](https://avatars.githubusercontent.com/u/15325886?v=4)](https://github.com/agustingomes "agustingomes (2 commits)")

---

Tags

ormdoctrineDomain Driven Designdispatcherdomain-event

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58523.9M36](/packages/scienta-doctrine-json-functions)[laravel-doctrine/orm

An integration library for Laravel and Doctrine ORM

8425.3M87](/packages/laravel-doctrine-orm)[damienharper/auditor-bundle

Integrate auditor library in your Symfony projects.

4542.8M](/packages/damienharper-auditor-bundle)[sonata-project/doctrine-orm-admin-bundle

Integrate Doctrine ORM into the SonataAdminBundle

46117.7M155](/packages/sonata-project-doctrine-orm-admin-bundle)[doctrine/doctrine-orm-module

Laminas Module that provides Doctrine ORM functionality

4407.3M293](/packages/doctrine-doctrine-orm-module)[damienharper/auditor

The missing audit log library.

1922.8M8](/packages/damienharper-auditor)

PHPackages © 2026

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