You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you clone an entity with an associated entity that is loaded as a Lazy Ghost Proxy, in certain circumstances, you lose the previous cloning functionality.
In most cases, with a clone, it is just a reference to the associated entity.
However, in oneToOne relationships, orm cannot keep the reference. You would have to duplicate the entity properties.
Preivous
With enable_lazy_ghost_objects: false, we used to have \Doctrine\ORM\Proxy\ProxyFactory::createCloner() to finalize proxy entities when cloned. This was deprecated. I would assume its functionality should be preserved.
Current
With enable_lazy_ghost_objects: true, we rely on \Doctrine\ORM\Proxy\ProxyFactory::getProxyFactory(). This has an initializer that will initialize identifiers correctly. However all other data is lost.
Current behavior
Lets say we have a Parent $parent entity with a Child $child entity with a OneToOne relationship.
And Child has a required property $name. and a auto generated ID property $id.
$parent->getChild(); // Proxy of $child. Empty. $clonedParent = clone$parent;
$clonedParent->getChild(); // Proxy of $child. Still Empty.$entityManager->persist($clonedParent);
$entityManager->flush() // Error! $name is null.
Expected behavior
$parent->getChild(); // Proxy of $child. Empty. $clonedParent = clone$parent;
$clonedParent->getChild(); // Proxy of $child. Still Empty.$entityManager->persist($clonedParent);
$entityManager->flush() // New $clonedParent persisted. New $child persisted with different ID and same properties. (Same as before).
Current workaround
You can workaround this by forcing the Proxied entity to fully initialize before cloning
$parent->getChild(); // Proxy of $child. Empty. $parent->getChild()->getName(); // Force orm to load Proxy.$parent->getChild(); // Now a fully loaded Proxy;$clonedParent = clone$parent;
$clonedParent->getChild(); // Proxy of $child. Fully loaded.$entityManager->persist($clonedParent);
$entityManager->flush() // Persisted as expected
Bug Report
Summary
When you clone an entity with an associated entity that is loaded as a Lazy Ghost Proxy, in certain circumstances, you lose the previous cloning functionality.
In most cases, with a clone, it is just a reference to the associated entity.
However, in oneToOne relationships, orm cannot keep the reference. You would have to duplicate the entity properties.
Preivous
With
enable_lazy_ghost_objects: false
, we used to have \Doctrine\ORM\Proxy\ProxyFactory::createCloner() to finalize proxy entities when cloned. This was deprecated. I would assume its functionality should be preserved.Current
With
enable_lazy_ghost_objects: true
, we rely on \Doctrine\ORM\Proxy\ProxyFactory::getProxyFactory(). This has an initializer that will initialize identifiers correctly. However all other data is lost.Current behavior
Lets say we have a
Parent $parent
entity with aChild $child
entity with aOneToOne
relationship.And Child has a required property
$name
. and a auto generated ID property $id.Expected behavior
Current workaround
You can workaround this by forcing the Proxied entity to fully initialize before cloning
How to reproduce
Reproducer > https://github.com/jonnyeom/clone-lazy-ghost-proxy-reproducer
Possible solution
I honestly think all we need is to restore the __clone() on the Proxy entity.
The text was updated successfully, but these errors were encountered: