Skip to content

Cannot persist an entity during onFlush #11892

@yann-eugone

Description

@yann-eugone

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions