DDC-3926: orphanRemovals remove entities w/o persist/flush #4801

Open
opened 2026-01-22 14:49:40 +01:00 by admin · 1 comment
Owner

Originally created by @doctrinebot on GitHub (Oct 1, 2015).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user po_taka:

Example:

Car entity:

/*
 * @ORM\OneToMany(targetEntity="Door", mappedBy="car", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $doors;

Example code:

$car = $repo->find(1);
// some code
$car->removeDoor($leftDoor);

$car2 = $repo->find(2);
$car2->setName('BMW');

$em->persist($car2);
$em->flush();

Bug: $leftDoor is removed from the DB, but $car1 is not persisted.

The problem is that when element is removed from PersistentCollection , then

$this->em->getUnitOfWork()->scheduleOrphanRemoval($element);

is called.
This add deletedEntity to UnitOfWork->orphanRemovals, this is ok.

But when any entity is flushed, uow use the same UnitOfWork->orphanRemovals to remove orphans. This could delete non-persisted entities like $car1.

@see
Unit of work problematic part: https://github.com/doctrine/doctrine2/blob/2.4/lib/Doctrine/ORM/UnitOfWork.php#L318
@see
stackoverflow similar problem: http://stackoverflow.com/questions/16575114/doctrine-2-entities-relations-remove/28405843#28405843

sry for bad English :/

Originally created by @doctrinebot on GitHub (Oct 1, 2015). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user po_taka: Example: Car entity: ``` /* * @ORM\OneToMany(targetEntity="Door", mappedBy="car", cascade={"persist", "remove"}, orphanRemoval=true) */ private $doors; ``` Example code: ``` $car = $repo->find(1); // some code $car->removeDoor($leftDoor); $car2 = $repo->find(2); $car2->setName('BMW'); $em->persist($car2); $em->flush(); ``` Bug: $leftDoor is removed from the DB, but $car1 is not persisted. The problem is that when element is removed from PersistentCollection , then ``` $this->em->getUnitOfWork()->scheduleOrphanRemoval($element); ``` is called. This add deletedEntity to UnitOfWork->orphanRemovals, this is ok. But when **any** entity is flushed, uow use the same UnitOfWork->orphanRemovals to remove orphans. This could delete non-persisted entities like $car1. @see Unit of work problematic part: https://github.com/doctrine/doctrine2/blob/2.4/lib/Doctrine/ORM/UnitOfWork.php#L318 @see stackoverflow similar problem: http://stackoverflow.com/questions/16575114/doctrine-2-entities-relations-remove/28405843#28405843 sry for bad English :/
admin added the Bug label 2026-01-22 14:49:40 +01:00
Author
Owner

@doctrinebot commented on GitHub (Oct 30, 2015):

Comment created by po_taka:

(11:46:20 AM) Topic for #doctrine set by guilhermeblanco!~quassel@nat/yahoo/x-oqorybaiqjudglte at 09:20:42 PM on 08/12/2010
(11:47:33 AM) po_taka: Hello, I reported a bug 30days ago.
(11:47:33 AM) po_taka: http://www.doctrine-project.org/jira/browse/[DDC-3926](http://www.doctrine-project.org/jira/browse/DDC-3926)
(11:47:33 AM) po_taka: Is there something wrong in my bug report ? Why there is no attention ?
(11:47:33 AM) po_taka: Anyone know a 'work-around' ?
(11:53:23 AM) jkavalik: po_taka, no idea about activity, but to the issue - $em->flush(); flushes entire state of the EM, not any single entity
(11:53:40 AM) jkavalik: so it does all the work scheduled for syncing
(11:54:56 AM) po_taka: but car1 is not persisted
(11:55:07 AM) jkavalik: po_taka, $car was fetched from the repo so it is "managed" and every change on it "counts", same with $car2; you call to "$em->persist($car2);" should not be needed at all, persist is only for NEW entities not yet managed
(11:55:26 AM) po_taka: ok, thanks
(11:55:28 AM) jkavalik: $car was already persisted some time prior as it exists in the repository
(11:56:27 AM) jkavalik: there is a possibility to call $em->flush($car2); to sync only a specific entity, but it is not properly documented IIRC, so at your own risk ;)
(11:56:57 AM) po_taka: tried, it still delete car1 doors :)
(11:58:31 AM) jkavalik: po_taka, https://github.com/doctrine/doctrine2/blob/2.4/lib/Doctrine/ORM/UnitOfWork.php#L288 - commit($entity) - yes, seems like some things do not depend on it, should be removed probably
@doctrinebot commented on GitHub (Oct 30, 2015): Comment created by po_taka: ``` (11:46:20 AM) Topic for #doctrine set by guilhermeblanco!~quassel@nat/yahoo/x-oqorybaiqjudglte at 09:20:42 PM on 08/12/2010 (11:47:33 AM) po_taka: Hello, I reported a bug 30days ago. (11:47:33 AM) po_taka: http://www.doctrine-project.org/jira/browse/[DDC-3926](http://www.doctrine-project.org/jira/browse/DDC-3926) (11:47:33 AM) po_taka: Is there something wrong in my bug report ? Why there is no attention ? (11:47:33 AM) po_taka: Anyone know a 'work-around' ? (11:53:23 AM) jkavalik: po_taka, no idea about activity, but to the issue - $em->flush(); flushes entire state of the EM, not any single entity (11:53:40 AM) jkavalik: so it does all the work scheduled for syncing (11:54:56 AM) po_taka: but car1 is not persisted (11:55:07 AM) jkavalik: po_taka, $car was fetched from the repo so it is "managed" and every change on it "counts", same with $car2; you call to "$em->persist($car2);" should not be needed at all, persist is only for NEW entities not yet managed (11:55:26 AM) po_taka: ok, thanks (11:55:28 AM) jkavalik: $car was already persisted some time prior as it exists in the repository (11:56:27 AM) jkavalik: there is a possibility to call $em->flush($car2); to sync only a specific entity, but it is not properly documented IIRC, so at your own risk ;) (11:56:57 AM) po_taka: tried, it still delete car1 doors :) (11:58:31 AM) jkavalik: po_taka, https://github.com/doctrine/doctrine2/blob/2.4/lib/Doctrine/ORM/UnitOfWork.php#L288 - commit($entity) - yes, seems like some things do not depend on it, should be removed probably ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4801