mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Misleading ORMInvalidArgumentException after removal of referred entity of ManyToOne association with onDelete: 'CASCADE' #7393
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?
Originally created by @tobiasgv on GitHub (Jul 2, 2024).
Bug Report
Summary
For a ManyToOne association where the owning side (class A) has the onDelete: 'CASCADE' option set: when a referred entity (of class B) is removed by the entity manager then a subsequent call to persist()+flush() causes a misleading ORMInvalidArgumentException:
Doctrine\ORM\ORMInvalidArgumentException: A new entity was found through the relationship 'A#b' that was not configured to cascade persist operations for entity: XXX. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).for managed entities of class A (which were cascade deleted by the DB), since the referenced entity of class B is mis-interpreted as a new entity.
How to reproduce
Expected behavior
No exception should be thrown.
Workaround
Unsetting the referenced entity before flushing prevents the exception:
$a->setB(null);@stof commented on GitHub (Jul 4, 2024):
The issue in your case is that
$astill reference$bin your UnitOfWork.onDelete: 'CASCADE'is about configuring the database-level cascading on the foreign key. This will lead to the row being deleted from the database in theatable, but the ORM still has$ain its UnitOfWork as it does not know about this removal.If your schema relies on
onDelete: 'CASCADE'(oronDelete: 'SET NULL'), you should be careful when you keep using the UnitOfWork after a flush. To be reliable, you would have to clear the UnitOfWork after a flush involving removals (as such flushes might have triggered anonDeletebehavior)@tobiasgv commented on GitHub (Jul 8, 2024):
Ok, thank you. I will clear the UnitOfWork after the removal.