Persisted Then Removed Entity Tries to Persist #6167

Closed
opened 2026-01-22 15:28:06 +01:00 by admin · 1 comment
Owner

Originally created by @bobdercole on GitHub (Feb 1, 2019).

Bug Report

Q A
BC Break no
Version 2.6.3

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 within UnitOfWork::computeAssociationChanges(). However at this point the entity state is identified as UnitOfWork::STATE_NEW, even though it's been removed via EntityManager::remove(). This causes the entity to remain in the inverse association collection. It also results in an exception due to unexpected persistence by reachability.

Uncaught Doctrine\\ORM\\ORMInvalidArgumentException: A new entity was found through the relationship...

I should note that this strange behavior only occurs if EntityManager::persist() and EntityManager::remove() are called within the same EntityManager::flush(). The issue does not occur if a second EntityManager::flush() is added between EntityManager::persist() and EntityManager::remove().

How to reproduce

  1. Declare an entity as the owning side of a one-to-many bidirectional relationship. The constructor needs to add itself to the inverse association collection.
  2. Instantiate the entity.
  3. Persist the entity via EntityManager::persist().
  4. Remove the entity via EntityManager::remove().
  5. Flush the entity manager.

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.

Originally created by @bobdercole on GitHub (Feb 1, 2019). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.6.3 #### Summary <!-- Provide a summary describing the problem you are experiencing. --> 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 <!-- What is the current (buggy) 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 within ```UnitOfWork::computeAssociationChanges()```. However at this point the entity state is identified as ```UnitOfWork::STATE_NEW```, even though it's been removed via ```EntityManager::remove()```. This causes the entity to remain in the inverse association collection. It also results in an exception due to unexpected persistence by reachability. ``` Uncaught Doctrine\\ORM\\ORMInvalidArgumentException: A new entity was found through the relationship... ``` I should note that this strange behavior only occurs if ```EntityManager::persist()``` and ```EntityManager::remove()``` are called within the same ```EntityManager::flush()```. The issue does not occur if a second ```EntityManager::flush()``` is added between ```EntityManager::persist()``` and ```EntityManager::remove()```. #### How to reproduce <!-- Provide steps to reproduce the bug. If possible, also add a code snippet with relevant configuration, entity mappings, DQL etc. Adding a failing Unit or Functional Test would help us a lot - you can submit one in a Pull Request separately, referencing this bug report. --> 1. Declare an entity as the owning side of a one-to-many bidirectional relationship. The constructor needs to add itself to the inverse association collection. 2. Instantiate the entity. 3. Persist the entity via ```EntityManager::persist()```. 4. Remove the entity via ```EntityManager::remove()```. 5. Flush the entity manager. Here is a test case: #7590. #### Expected behavior <!-- What was the expected (correct) 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.
admin closed this issue 2026-01-22 15:28:06 +01:00
Author
Owner

@bobdercole commented on GitHub (Feb 4, 2019):

Closed due to user error (see #7590 review conversation).

@bobdercole commented on GitHub (Feb 4, 2019): Closed due to user error (see #7590 review conversation).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6167