feat: add withoutDoctrineEvents() to suppress listeners during factory create#1109
Open
seb-jean wants to merge 1 commit intozenstruck:2.xfrom
Open
feat: add withoutDoctrineEvents() to suppress listeners during factory create#1109seb-jean wants to merge 1 commit intozenstruck:2.xfrom
withoutDoctrineEvents() to suppress listeners during factory create#1109seb-jean wants to merge 1 commit intozenstruck:2.xfrom
Conversation
6266ed9 to
9ebb1d6
Compare
nikophil
reviewed
Mar 25, 2026
Member
nikophil
left a comment
There was a problem hiding this comment.
hi @seb-jean
thanks to work on this! I have the need for this feature from time to time
Could you please add some more test cases:
- one using
#[ORM\EntityListeners]on the entity - one using
#[AsEntityListener]on a listener - one using
flush_after()(I think you'll have to change some code to make it work)
Here are some questions, maybe for the future:
- is it possible to make it work with
#[ORM\HasLifecycleCallbacks]? - do you think it's possible to disable all listeners for a given event for an entity? meaning something like
->disableDoctrineEvents('prePersist')
93b6662 to
76a4796
Compare
76a4796 to
8c5ae0b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When Foundry factories create entities during fixture loading, Doctrine event listeners attached to those entities fire as usual. This can cause unwanted side effects in tests — for example, sending emails, dispatching messages, or calling external services on every persist.
Related #216.
What this PR adds
A
withoutDoctrineEvents()method onPersistentObjectFactorythat temporarily removes Doctrine event listeners from theEventManageraround theflush()call, then restores them immediately after.API
I have 2 failing checks, but both are pre-existing issues unrelated to this PR.
fixes #957