[PR #5848] Pre-flush event knows which entities are being flushed #9755

Open
opened 2026-01-22 16:05:19 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/5848

State: closed
Merged: No


short description
This PR focuses on adding the entities which are being flushed for explicit flushes to the PreFlushEvent (and if desired I can add other events too)

A bit of background info
For an event listener we need to know which fields have changed in an entity and we fire a custom entity changed event in case the entity is tracked. For normal requests this works fine but we hit a bottleneck in larger batches. The challenge we are facing is that we need to know if:

  • The entity is tracked by our listener (@Tracked annotation)
  • The entity state is changed (1 level deep, not recursive)

We're doing this pre-flush so that additional changes/new entities can be created and added to the 'to-be-changed-set', which results in being unable to use the doctrine changeset computation as it would run twice. We've created a lightweight variant for this but this is called for every single entity. We also create a new "entity" and use the hydration to create as it was in its original state when hydrated for the first time. This means we can compare/absorb the objects and know the exact changes and pass this along to our event: https://github.com/hostnet/entity-tracker-component/blob/master/src/Listener/EntityChangedListener.php

As we need to know which entities have changed to calculate the mutations and fire events, we have to check the internal state of the Unit of Work and calculate this. This is what you would expect from a flush(). However, if you have 1000 entities in memory and an explicit flush is done flush(...) or flush([...]); you already know which entities will be flushed. This makes it meaningless to iterate over all managed entities.

If this change is something you want to add, I will add tests (can't run the tests locally).

**Original Pull Request:** https://github.com/doctrine/orm/pull/5848 **State:** closed **Merged:** No --- **short description** This PR focuses on adding the entities which are being flushed for explicit flushes to the PreFlushEvent (and if desired I can add other events too) **A bit of background info** For an event listener we need to know which fields have changed in an entity and we fire a custom entity changed event in case the entity is tracked. For normal requests this works fine but we hit a bottleneck in larger batches. The challenge we are facing is that we need to know if: - The entity is tracked by our listener (`@Tracked` annotation) - The entity state is changed (1 level deep, not recursive) We're doing this pre-flush so that additional changes/new entities can be created and added to the 'to-be-changed-set', which results in being unable to use the doctrine changeset computation as it would run twice. We've created a lightweight variant for this but this is called for every single entity. We also create a new "entity" and use the hydration to create as it was in its original state when hydrated for the first time. This means we can compare/absorb the objects and know the exact changes and pass this along to our event: https://github.com/hostnet/entity-tracker-component/blob/master/src/Listener/EntityChangedListener.php As we need to know which entities have changed to calculate the mutations and fire events, we have to check the internal state of the Unit of Work and calculate this. This is what you would expect from a `flush()`. However, if you have 1000 entities in memory and an explicit flush is done `flush(...)` or `flush([...]);` you already know which entities will be flushed. This makes it meaningless to iterate over all managed entities. If this change is something you want to add, I will add tests (can't run the tests locally).
admin added the pull-request label 2026-01-22 16:05:19 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#9755