[PR #1546] Fix proxy erased by hydratation #9603

Open
opened 2026-01-22 16:04:49 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/1546

State: closed
Merged: No


This PR fixe a edge case when hydrating an existing object which already has a proxy to différente entity.

Imagine the entity

/** @ORM\Entity() */
class User
{
    //...

    /** @ORM\ManyToOne(targetEntity="Address", fetch="EAGER") */
    private $address;

    //...
}

/** @ORM\Entity() */
class Address
{
}

When a instance of User has an address created with a Proxy, then, the hydratation of an other instance of the same User will replace the content of the field address in this instance.

Such situation append with a PUT call with the DunglasApiBundle and the validator UniqueEntity from Symfony which trigger a $repository->find on the object.

  • First the entity is loaded from the entityManager (which store the reference in the identityMap of the uow
  • Then the serializer overrwrite the entity, and may create reference for the related properties
  • Then the object is validated before flushing which trigger the UniqueEntityValidator

How to reproduce:

$user = $repository->find($id);
var_dump($user->getAddress()->getId());
// 1337

$user->setAddress($em->getReference('Address', 42));
var_dump($user->getAddress()->getId());
// 42

$somethingElse = $repository->find($id);
var_dump($user->getAddress()->getId());
// 1337
**Original Pull Request:** https://github.com/doctrine/orm/pull/1546 **State:** closed **Merged:** No --- This PR fixe a edge case when hydrating an existing object which already has a proxy to différente entity. Imagine the entity ``` /** @ORM\Entity() */ class User { //... /** @ORM\ManyToOne(targetEntity="Address", fetch="EAGER") */ private $address; //... } /** @ORM\Entity() */ class Address { } ``` When a instance of `User` has an address created with a `Proxy`, then, the hydratation of an other instance of the same `User` will replace the content of the field `address` in this instance. Such situation append with a PUT call with the [DunglasApiBundle](https://github.com/dunglas/DunglasApiBundle) and the validator [UniqueEntity](https://github.com/symfony/symfony/blob/2.8/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php) from Symfony which trigger a `$repository->find` on the object. - First the entity is loaded from the entityManager (which store the reference in the `identityMap` of the `uow` - Then the serializer overrwrite the entity, and may create reference for the related properties - Then the object is validated before flushing which trigger the UniqueEntityValidator How to reproduce: ``` $user = $repository->find($id); var_dump($user->getAddress()->getId()); // 1337 $user->setAddress($em->getReference('Address', 42)); var_dump($user->getAddress()->getId()); // 42 $somethingElse = $repository->find($id); var_dump($user->getAddress()->getId()); // 1337 ```
admin added the pull-request label 2026-01-22 16:04:49 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#9603