DDC-3343: PersistentCollection::removeElement schedules an entity for deletion when relationship is EXTRA_LAZY, with orphanRemoval false. #4133

Closed
opened 2026-01-22 14:35:49 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 9, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user asprega:

Given the following entity for which I only report the relevant association:

class Group
{
    /****
     * @ORM\OneToMany(targetEntity="User", mappedBy="group", fetch="EXTRA_LAZY")
     */
    protected $users;
    //...
}

and its inverse

class User
{
    /****
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="users")
     * @ORM\JoinColumn(name="group_id", onDelete="SET NULL")
     */
    protected $group;
    //...
}

I experience a weird issue when, inside Group, I call $this->users->removeElement($user): Doctrine schedules the $user entity for deletion, no matter what (it doesn't check for the orphanRemoval attribute). Also, even re-adding the entity to a new Group before the flush() occurs doesn't prevent it from being deleted.

I believe this is a bug, since I do not see any special mention of this behavior in the documentation for extra lazy associations:
http://doctrine-orm.readthedocs.org/en/latest/tutorials/extra-lazy-associations.html.

The workaround for me has been to remove fetch="EXTRA_LAZY" from the relationship.

After digging a little bit into the source code, this seems to be the commit (and the line) that introduced this behavior:

356f5874bf (diff-108586f774fc8acb02163ed709e05e86R403)

I also found this on Google:

https://groups.google.com/forum/#!topic/doctrine-user/cx_yBuoqiAE

which basically didn't help much, except for suggesting that it should be a feature when there is an orphanRemoval and/or a cascade operation in place (and that makes perfectly sense).

I set this to "critical" since (as it occurred to me) it can lead to irrecoverable data loss.

Is this effectively a bug?

Originally created by @doctrinebot on GitHub (Oct 9, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user asprega: Given the following entity for which I only report the relevant association: ``` class Group { /**** * @ORM\OneToMany(targetEntity="User", mappedBy="group", fetch="EXTRA_LAZY") */ protected $users; //... } ``` and its inverse ``` class User { /**** * @ORM\ManyToOne(targetEntity="Group", inversedBy="users") * @ORM\JoinColumn(name="group_id", onDelete="SET NULL") */ protected $group; //... } ``` I experience a weird issue when, inside `Group`, I call `$this->users->removeElement($user)`: Doctrine schedules the $user entity for deletion, no matter what (it doesn't check for the `orphanRemoval` attribute). Also, even re-adding the entity to a new `Group` before the `flush()` occurs doesn't prevent it from being deleted. I believe this is a bug, since I do not see any special mention of this behavior in the documentation for extra lazy associations: http://doctrine-orm.readthedocs.org/en/latest/tutorials/extra-lazy-associations.html. The workaround for me has been to remove `fetch="EXTRA_LAZY"` from the relationship. After digging a little bit into the source code, this seems to be the commit (and the line) that introduced this behavior: https://github.com/doctrine/doctrine2/commit/356f5874bf81ca4e37681c233e24cc84d16e7a39#diff-108586f774fc8acb02163ed709e05e86R403 I also found this on Google: https://groups.google.com/forum/#!topic/doctrine-user/cx_yBuoqiAE which basically didn't help much, except for suggesting that it should be a feature when there is an orphanRemoval and/or a cascade operation in place (and that makes perfectly sense). I set this to "critical" since (as it occurred to me) it can lead to irrecoverable data loss. Is this effectively a bug?
admin added the Bug label 2026-01-22 14:35:49 +01:00
admin closed this issue 2026-01-22 14:35:49 +01:00
Author
Owner
@doctrinebot commented on GitHub (Oct 9, 2014): - depends on [DDC-3536: [GH-1281] Hotfix/#1169 extra lazy one to many should not delete referenced entities](http://www.doctrine-project.org/jira/browse/DDC-3536) - depends on [DDC-3544: [GH-1288] Hotfix - #1169 - extra lazy one to many must be no-op when not doing orphan removal](http://www.doctrine-project.org/jira/browse/DDC-3544) - depends on [DDC-3560: [GH-1300] [2.4] #1169 DDC-3343 one-to-omany persister deletes only on EXTRA_LAZY plus orphanRemoval](http://www.doctrine-project.org/jira/browse/DDC-3560) - depends on [DDC-3537: [GH-1282] Hotfix/#1169 extra lazy one to many should not delete referenced entities (backport to 2.4)](http://www.doctrine-project.org/jira/browse/DDC-3537)
Author
Owner

@doctrinebot commented on GitHub (Jan 25, 2015):

Comment created by @ocramius:

Fixed via DDC-3536 and DDC-3537

@doctrinebot commented on GitHub (Jan 25, 2015): Comment created by @ocramius: Fixed via [DDC-3536](http://www.doctrine-project.org/jira/browse/DDC-3536) and [DDC-3537](http://www.doctrine-project.org/jira/browse/DDC-3537)
Author
Owner

@doctrinebot commented on GitHub (Jan 25, 2015):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Jan 25, 2015): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Sep 2, 2015):

Comment created by thinkscape:

Thanks!

@doctrinebot commented on GitHub (Sep 2, 2015): Comment created by thinkscape: Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4133