Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot persist an entity during onFlush #11892

Open
yann-eugone opened this issue Mar 27, 2025 · 2 comments
Open

Cannot persist an entity during onFlush #11892

yann-eugone opened this issue Mar 27, 2025 · 2 comments

Comments

@yann-eugone
Copy link

yann-eugone commented Mar 27, 2025

Bug Report

Q A
Version 3.3.2
Previous Version if the bug is a regression

Summary

Not able to persist a new entity in the onFlush listener.

Current behavior

When using EntityManager::persist() along with UnitOfWork::computeChangeSet(), I get a SQL syntax error: the insert query does not have any parameter bound.

When using Collection::add() along with UnitOfWork::recomputeSingleEntityChangeSet(), nothing happens: the entity is never saved.

Expected behavior

Regarding the documentation, we should be able to persist new entities, on their own, or using collections:

The following restrictions apply to the onFlush event:

  • If you create and persist a new entity in onFlush, then calling EntityManager::persist() is not enough. You have to execute an additional call to $unitOfWork->computeChangeSet($classMetadata, $entity).
  • Changing primitive fields or associations requires you to explicitly trigger a re-computation of the changeset of the affected entity. This can be done by calling $unitOfWork->recomputeSingleEntityChangeSet($classMetadata, $entity).

How to reproduce

I created a pretty simple project with my use case
https://github.com/yann-eugone/doctrine-persist-in-onflush

The idea is that I have a AuditEvent entity that should be created every time an entity is updated.

@mpdude
Copy link
Contributor

mpdude commented Mar 30, 2025

#10900 is related.

Are you seeing #10869 here?

@yann-eugone
Copy link
Author

Not sure it is related, not doing any flush during the event listener
Tried to downgrade doctrine/orm on the project, never been able to see it work as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants