#12642·orm

orphanRemoval deletes an entity that is still in the collection (remove() does not re-check membership)

Author: greedyivanCreated Sep 30, 2026Updated Sep 30, 2026

Bug Report

Q A
Doctrine ORM version 3.7.x-dev (current 3.7.x head, 2637802f3)
PHP version 8.4 (script note below)

Summary

An element can be in a one-to-many collection more than once — added twice with add(), or held under two indexBy keys. Dropping one occurrence with remove() deletes the entity's row at flush, although the element never left the collection.

Current behavior

PersistentCollection::remove() schedules the removed element for orphan removal without re-checking whether it is still held by the collection, and nothing re-checks membership before the orphan materializes in UnitOfWork::commit() — so a still-member entity is deleted.

How to reproduce

php
<?php

declare(strict_types=1);

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\Tools\SchemaTool;

require __DIR__ . '/vendor/autoload.php';

#[ORM\Entity]
class Person
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    public int|null $id = null;

    /** @var Collection<int, Email> */
    #[ORM\OneToMany(targetEntity: Email::class, mappedBy: 'owner', cascade: ['persist'], orphanRemoval: true)]
    public Collection $emails;

    public function __construct()
    {
        $this->emails = new ArrayCollection();
    }
}

#[ORM\Entity]
class Email
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    public int|null $id = null;

    #[ORM\ManyToOne(targetEntity: Person::class, inversedBy: 'emails')]
    #[ORM\JoinColumn(name: 'owner_id', referencedColumnName: 'id')]
    public Person|null $owner = null;

    public function __construct(Person $owner)
    {
        $this->owner = $owner;
    }
}

$config = ORMSetup::createAttributeMetadataConfiguration([__DIR__], true);
$config->enableNativeLazyObjects(true);
$config->setProxyDir(__DIR__ . '/proxies');
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true], $config);
$em         = new EntityManager($connection, $config);

(new SchemaTool($em))->createSchema([
    $em->getClassMetadata(Person::class),
    $em->getClassMetadata(Email::class),
]);

$person = new Person();
$person->emails->add(new Email($person));

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

$personId = $person->id;
$emailId  = $person->emails->first()->id;

$em->clear();

$person = $em->find(Person::class, $personId);
$person->emails->add($person->emails->first()); // the element is now in the collection twice
$person->emails->remove(0);                     // ... and still is, after this remove()

$em->flush();
$em->clear();

printf(
    "elements after reload: %d\n",
    count($em->find(Person::class, $personId)->emails),
);
printf(
    "row of the still-member email: %s\n",
    $em->find(Email::class, $emailId) !== null ? 'still present' : 'DELETED',
);

Output on the current 3.7.x head:

elements after reload: 0
row of the still-member email: DELETED

With indexBy the same happens through set('work', $homeEmail) + remove('home') while the element is still under 'work'. (enableNativeLazyObjects(true) requires PHP 8.4; on older PHP drop that call and make sure symfony/var-exporter 6.4 or 7 is installed — version 8 removed the lazy-ghost support the proxy path needs.)

Expected behavior

The entity survives: it is still referenced by the collection. Orphan removal is documented for entities that are not used anymore (docs/en/reference/working-with-associations.rst), and re-adding an element after a remove() already cancels its scheduled orphan removal (cancelOrphanRemoval(), DDC-3382 / #4184) — being in the collection means it is not an orphan.

Additional context

Found while writing tests for #12641, which adds the membership check for set(); remove() and removeElement() schedule without it. Extending the same membership check to the element-wise mutators closes this gap.