Open tobiasgv opened 5 days ago
The issue in your case is that $a
still reference $b
in 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 the a
table, but the ORM still has $a
in its UnitOfWork as it does not know about this removal.
If your schema relies on onDelete: 'CASCADE'
(or onDelete: '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 an onDelete
behavior)
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);