Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cloning entities when using lazy-ghost proxies #10819

Merged
merged 1 commit into from
Jul 6, 2023

Conversation

nicolas-grekas
Copy link
Member

Fixes #10817
Replaces #10818
Replaces symfony/symfony#50899

@@ -98,6 +86,9 @@ public function __serialize(): array
*/
private $identifierFlattener;

/** @var ProxyDefinition[] */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like array<class-string, ProxyDefinition>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I borrowed from the parent class ;)

@nicolas-grekas
Copy link
Member Author

All green 🍏

@greg0ire greg0ire added this to the 2.15.4 milestone Jul 6, 2023
@greg0ire greg0ire added the Bug label Jul 6, 2023
@greg0ire greg0ire merged commit 4978e0e into doctrine:2.15.x Jul 6, 2023
@greg0ire
Copy link
Member

greg0ire commented Jul 6, 2023

Thanks @nicolas-grekas !

@mpdude
Copy link
Contributor

mpdude commented Jul 7, 2023

Can you guys provide an example when/how it is useful to clone entities and what the semantics of this operation are?

Does the new (cloned) entity let go of its ID? Should the EntityManager know about it? Is it in MANAGED or DETACHED state etc.?

@nicolas-grekas nicolas-grekas deleted the fix-proxy-clone branch July 7, 2023 11:56
@stof
Copy link
Member

stof commented Jul 7, 2023

@mpdude the cloned object will not be managed by the entity manager

@mpdude
Copy link
Contributor

mpdude commented Jul 7, 2023

When the entity that is cloned is an uninitialized proxy at that point in time, what happens when I call a method on the clone that triggers loading?

@mpdude
Copy link
Contributor

mpdude commented Jul 7, 2023

Coming from #3037 and #10785 where one source of "weird" problems is corruption of internal UoW state-tracking fields. This may be caused when using proxies directly created by the ProxyFactory.

My concern is that cloning a proxy object might be no different:

Suddenly there are two objects claiming to proxy the same entity. But, "there can be only one", only the proxy object the UoW knows about should be initialized and is the sole object representing that entity.

@GrimReaper1908
Copy link

When the entity that is cloned is an uninitialized proxy at that point in time, what happens when I call a method on the clone that triggers loading?

I stumbled upon this while investigating an issue that seems to be related to this fix, and your suspicion seems to be confirmed.

Since doctrine/orm 2.15.4 I encounter the following problem:

  • I have a lazy-loaded entity original
  • I clone original into clone
  • I read some data from original, causing it to load the remaining data
  • I modify some data on original

Up until now, everything is as expected. However, once I read some data from clone , it reads the missing data back from the database and populates it into clone and original, causing the modifications on original to be lost.

@nicolas-grekas
Copy link
Member Author

Please open a dedicated issue with a reproducer. Commenting on closed issues/PRs isn't tracked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong LazyGhost initialization on cloned entity
5 participants