mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
[PR #5848] Pre-flush event knows which entities are being flushed #9755
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
@Trackedannotation)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 doneflush(...)orflush([...]);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).