PHPackages                             bentools/doctrine-safe-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. bentools/doctrine-safe-events

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bentools/doctrine-safe-events
=============================

Fires postPersist / postUpdate / postRemove events AFTER the transaction has completed.

1.2.0(2mo ago)212.7k↓84.1%MITPHPPHP &gt;=8.2CI passing

Since Jan 4Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/bpolaszek/doctrine-safe-events)[ Packagist](https://packagist.org/packages/bentools/doctrine-safe-events)[ RSS](/packages/bentools-doctrine-safe-events/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (4)Dependencies (14)Versions (6)Used By (0)

[![CI Workflow](https://github.com/bpolaszek/doctrine-safe-events/actions/workflows/ci.yml/badge.svg)](https://github.com/bpolaszek/doctrine-safe-events/actions/workflows/ci.yml)[![Coverage](https://camo.githubusercontent.com/75fca47a7afc608623a0779687393d2ded76e6aa92eadd622e2e52541a666daf/68747470733a2f2f636f6465636f762e696f2f67682f62706f6c61737a656b2f646f637472696e652d736166652d6576656e74732f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d6f326b43434975587a33)](https://codecov.io/gh/bpolaszek/doctrine-safe-events)

Doctrine safe post\* events
===========================

[](#doctrine-safe-post-events)

Doctrine's [postPersist, postUpdate and postRemove](https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/events.html#reference-events-post-update-remove-persist) events are fired when the corresponding SQL queries (INSERT / UPDATE / DELETE) have been performed against the database server.

What happens under the hood is that Doctrine creates a wrapping transaction, runs SQL queries, then commits the transaction.

However, these events are fired *immediately*, e.g. not once the transaction is **complete**, which means:

- If the wrapping transaction fails, events have **already been fired anyway** (meaning you trusted generated primary key values, although they're going to be rolled back)
- If the wrapping transaction takes some time (typically during row locks), you get the inserted / updated / deleted information **before it's actually done**(meaning if you run some async process once those events are triggered, you end up in processing not data which is not **up-to-date**)

Doctrine ORM documentation states:

Warning

At the time `postPersist` is called, there may still be collection and/or "extra" updates pending. The database **may not yet be completely in sync with the entity states** in memory, **not even for the new entities**. Similarly, also at the time `postUpdate` and `postRemove` are called, in-memory collections may still be in a "dirty" state or still contain removed entities.

Background
----------

[](#background)

The idea of this repository indeed came up with the following issue:

- An entity is persisted, then `$em->flush()` is called
- A `postPersist` event listener gets the entity's id, then asks a worker to do some async processing through [Symfony Messenger](https://symfony.com/doc/current/messenger.html)
- The worker queries database against the entity's id, and gets an `EntityNotFound` exception (the `COMMIT` did not happen yet)
- The flush operation on the main thread completes, and the `postFlush` event is fired (but it does not contain the inserted / updated / deleted entities)

Our solution
------------

[](#our-solution)

If you run into the same kind of issues, you can replace your listeners' listened events in favor of:

- `Bentools\DoctrineSafeEvents\SafeEvents::POST_PERSIST` (and implement `safePostPersist` as a replacement of `postPersist`)
- `Bentools\DoctrineSafeEvents\SafeEvents::POST_UPDATE` (and implement `safePostUpdate` as a replacement of `postUpdate`)
- `Bentools\DoctrineSafeEvents\SafeEvents::POST_REMOVE` (and implement `safePostRemove` as a replacement of `postRemove`)

Basically, this library will collect entities which are scheduled for insertion / update / deletion, except it will delay event firing until the `postFlush` occurs.

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

[](#installation)

```
composer require bentools/doctrine-safe-events
```

### Usage in Symfony

[](#usage-in-symfony)

Although this library has no dependency on Symfony, you can easily use it in your Symfony project:

```
Bentools\DoctrineSafeEvents\SafeEventsDispatcher:
    tags:
        - { name: 'doctrine.event_subscriber' }
        - { name: 'kernel.reset', method: 'reset' }
```

#### Example usage

[](#example-usage)

```
declare(strict_types=1);

namespace App;

use Bentools\DoctrineSafeEvents\SafeEvents;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\Persistence\Event\LifecycleEventArgs;

#[AsDoctrineListener(SafeEvents::POST_PERSIST)]
final class SomeListener
{
    public function safePostPersist(LifecycleEventArgs $eventArgs): void
    {
        // ...
    }
}
```

Tests
-----

[](#tests)

```
composer test
```

License
-------

[](#license)

MIT.

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance83

Actively maintained with recent releases

Popularity28

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 57.1% 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.

###  Release Activity

Cadence

Every ~397 days

Total

4

Last Release

85d ago

PHP version history (3 changes)1.0PHP &gt;=7.4

1.1.0PHP &gt;=8.0

1.2.0PHP &gt;=8.2

### Community

Maintainers

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

---

Top Contributors

[![misaert](https://avatars.githubusercontent.com/u/12974251?v=4)](https://github.com/misaert "misaert (4 commits)")[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (3 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bentools-doctrine-safe-events/health.svg)

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

###  Alternatives

[rcsofttech/audit-trail-bundle

Enterprise-grade, high-performance Symfony audit trail bundle. Automatically track Doctrine entity changes with split-phase architecture, multiple transports (HTTP, Queue, Doctrine), and sensitive data masking.

1189.8k](/packages/rcsofttech-audit-trail-bundle)[2lenet/crudit-bundle

The easy like Crud'it Bundle.

1616.4k14](/packages/2lenet-crudit-bundle)

PHPackages © 2026

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