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. bentools/doctrine-safe-events

ActiveLibrary

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

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

1.0.1(3y ago)211.3k↓50%1[2 issues](https://github.com/bpolaszek/doctrine-safe-events/issues)[4 PRs](https://github.com/bpolaszek/doctrine-safe-events/pulls)MITPHPPHP &gt;=7.4CI passing

Since Jan 4Pushed 1mo 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 1mo ago

READMEChangelog (2)Dependencies (8)Versions (4)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**)

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

35

—

LowBetter than 80% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% 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 ~0 days

Total

2

Last Release

1230d ago

### Community

Maintainers

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

---

Top Contributors

[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (2 commits)")[![misaert](https://avatars.githubusercontent.com/u/12974251?v=4)](https://github.com/misaert "misaert (1 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

[sonata-project/entity-audit-bundle

Audit for Doctrine Entities

644989.8k1](/packages/sonata-project-entity-audit-bundle)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[api-platform/doctrine-orm

Doctrine ORM bridge

243.1M39](/packages/api-platform-doctrine-orm)[neos/flow-development-collection

Flow packages in a joined repository for pull requests.

144179.3k3](/packages/neos-flow-development-collection)[pixelfederation/doctrine-resettable-em-bundle

Symfony bundle for decorating default entity managers using a resettable decorator.

20113.5k](/packages/pixelfederation-doctrine-resettable-em-bundle)[leapt/core-bundle

Symfony LeaptCoreBundle

2529.1k4](/packages/leapt-core-bundle)

PHPackages © 2026

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