mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Persisted Then Removed Entity Tries to Persist #6167
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 @bobdercole on GitHub (Feb 1, 2019).
Bug Report
Summary
I found an issue while attempting to instantiate a new entity, persist, and remove it all in the same flush. This results in an exception on flush.
This entity is the owning side of a one-to-many bidirectional relationship. It gets added to the inverse association collection when it's instantiated.
Current behavior
As mentioned above, the entity gets added to the inverse association collection when it's instantiated. If the entity is then removed with
EntityManager::remove(), it does not immediately get removed from the inverse association collection. This is fine, since the removal should occur withinUnitOfWork::computeAssociationChanges(). However at this point the entity state is identified asUnitOfWork::STATE_NEW, even though it's been removed viaEntityManager::remove(). This causes the entity to remain in the inverse association collection. It also results in an exception due to unexpected persistence by reachability.I should note that this strange behavior only occurs if
EntityManager::persist()andEntityManager::remove()are called within the sameEntityManager::flush(). The issue does not occur if a secondEntityManager::flush()is added betweenEntityManager::persist()andEntityManager::remove().How to reproduce
EntityManager::persist().EntityManager::remove().Here is a test case: #7590.
Expected behavior
I believe that an entity should not be persisted if
EntityManager::remove()is called on it. Furthermore, the entity should be removed from the inverse association collection on flush.@bobdercole commented on GitHub (Feb 4, 2019):
Closed due to user error (see #7590 review conversation).