mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Incorrect / lost cloning of an associated entity with LazyGhosts #7460
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jonnyeom on GitHub (Jan 8, 2025).
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 $parententity with aChild $childentity with aOneToOnerelationship.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.
@jonnyeom commented on GitHub (Jan 9, 2025):
Here is the reproducer > https://github.com/jonnyeom/clone-lazy-ghost-proxy-reproducer
@Nilz11 commented on GitHub (Oct 9, 2025):
This also happens in doctrine/mongodb-odm and data is lost completely on cloning.
@Nilz11 commented on GitHub (Oct 9, 2025):
@jonnyeom Could you find a better workaround?
@mpdude commented on GitHub (Oct 10, 2025):
@beberlei is cloning entities a valid and supported use case in the first place?
@jonnyeom commented on GitHub (Oct 13, 2025):
No I havent!
I just have an extra line of code to ensure initialization. and commented this issue for now. Its a little tacky but its working for us!