Entity references break change computing
Bug Report
| Q | A |
|---|---|
| Version | 3.6.0 |
| Previous Version if the bug is a regression | - |
Summary
Unexpected behavior when manipulating an entity reference.
Current behavior
I get a reference of my entity with $entity = $entityManager->getReference(MyEntity::class, $id).
Then I remove an existing OneToOne relation: $entity->entityB = null. This relation is configured to remove the orphan entity.
When I flush ($entityManager->flush()), nothing happens.
Expected behavior
When I flush, I expect the UnitOfWork to compute the change and trigger a DELETE query on the entityB.
How to reproduce
#[ORM\Entity]
class EntityA
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
public ?int $id = null;
#[ORM\OneToOne(orphanRemoval: true)]
public ?EntityB $entityB = null
}
#[ORM\Entity]
class EntityB
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
public ?int $id = null;
}
/** @var EntityManagerInterface $em */
$entityA = new EntityA();
$entityB = new EntityB();
$entityA->entityB = $entityB;
$em->persist($entityA);
$em->persist($entityB);
$em->flush();
$id = $entityA->id;
$em->clear();
unset($entityA, $entityB);
$entityA = $em->getReference(EntityA, $id);
$entityA->entityB = null;
$em->flush();Some clues
I found that in the unit of work, the attribute originalEntityData is used to compute changes.
If my entity is fetched with the classic getRepository(...)->find(...), this array contains the values for the fetched entity.
But if I use getReference(...), then load data by calling some attribute of it, data are loaded in my entity but the entry in originalEntityData for this entity is an empty array.
Source: doctrine/orm