Skip to content

feat: add withoutDoctrineEvents() to suppress listeners during factory create#1109

Open
seb-jean wants to merge 1 commit intozenstruck:2.xfrom
seb-jean:without-doctrine-events
Open

feat: add withoutDoctrineEvents() to suppress listeners during factory create#1109
seb-jean wants to merge 1 commit intozenstruck:2.xfrom
seb-jean:without-doctrine-events

Conversation

@seb-jean
Copy link
Copy Markdown
Contributor

@seb-jean seb-jean commented Mar 24, 2026

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 on PersistentObjectFactory that temporarily removes Doctrine event listeners from the EventManager around the flush() call, then restores them immediately after.

API

// Disable ALL Doctrine event listeners during create()
PostFactory::new()->withoutDoctrineEvents()->create([...]);

// Disable specific listeners only
PostFactory::new()
    ->withoutDoctrineEvents(MyEventListener::class)
    ->create([...]);

I have 2 failing checks, but both are pre-existing issues unrelated to this PR.

fixes #957

Copy link
Copy Markdown
Member

@nikophil nikophil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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')

@seb-jean seb-jean force-pushed the without-doctrine-events branch 2 times, most recently from 93b6662 to 76a4796 Compare March 25, 2026 09:17
@seb-jean seb-jean force-pushed the without-doctrine-events branch from 76a4796 to 8c5ae0b Compare March 25, 2026 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] allow factories to disable doctrine events

2 participants