Skip to content

Fix native lazy object change tracking#12434

Open
Brewal wants to merge 1 commit into
doctrine:3.6.xfrom
Brewal:fix-uow-native-lazy-objects
Open

Fix native lazy object change tracking#12434
Brewal wants to merge 1 commit into
doctrine:3.6.xfrom
Brewal:fix-uow-native-lazy-objects

Conversation

@Brewal

@Brewal Brewal commented Apr 17, 2026

Copy link
Copy Markdown

Fixes #12403

Native lazy objects may be registered as managed with an empty original-data snapshot while they are still uninitialized.

When Doctrine later sets their remaining lazy properties, PHP can mark the object as initialized without refreshing UnitOfWork original data. Change-set computation then treats the empty snapshot as a partial load and skips all fields.

Record original data when Doctrine initializes such objects through association wiring, and fall back to an update changeset if an initialized native lazy object still has an empty snapshot.

Added regression coverage for:

  • a managed one-to-one association target that becomes initialized with
    no original-data snapshot
  • a scalar change on an initialized native lazy object that previously
    was not persisted

Native lazy objects may be registered as managed with an empty
original-data snapshot while they are still uninitialized.

When Doctrine later sets their remaining lazy properties, PHP can mark
the object as initialized without refreshing UnitOfWork original data.
Change-set computation then treats the empty snapshot as a partial load
and skips all fields.

Record original data when Doctrine initializes such objects through
association wiring, and fall back to an update changeset if an
initialized native lazy object still has an empty snapshot.

Fixes doctrine#12403
@Brewal Brewal force-pushed the fix-uow-native-lazy-objects branch from 9b88eb7 to 0a3f5b7 Compare April 17, 2026 13:57
@beberlei

Copy link
Copy Markdown
Member

As this is an extremely low level and complex change, did you use AI for this change? Its fine if you do, but please be transparent about it.

@Brewal

Brewal commented Apr 23, 2026

Copy link
Copy Markdown
Author

@beberlei Yes absolutly, I did use AI mainly to help me understand the core logic of the unit of work, write the tests and have a big picture of the changes that we needed to do in the unit of work. I should have make that clear sorry.

After reviewing the changes, the tests looked quite good to me and are easy to understand. As for the changes in the unit of work, the main "fix" is here :

https://github.com/doctrine/orm/pull/12434/changes#diff-2cbf87f941a104db9881228f86e16e3c9f894a88c7ac9245b0632de36b8b887cR2561

The added functions are mainly written to re-use code logic. I'm very open to changes in this PR or even closing it. I did it in the first place because it's quite a important bug IMO for anyone using the new native lazy objects feature.

@lendinvest-egg

Copy link
Copy Markdown

We too have this issue when using native lazy objects, the UnitOfWork does not detect the ChangeSet and entities are not persisted.

This PR appears to resolve the issue, but we are blocked from resolving the Class \"Doctrine\\ORM\\Proxy\\Autoloader\" is deprecated. Use native lazy objects instead. deprecation until this is merged in.

@beberlei Can this PR be progressed?

@Brewal

Brewal commented Jul 4, 2026

Copy link
Copy Markdown
Author

Comming back on this issue, I found a way to workaround this issue by skipping totally native objects on this version, but if it was not clear enough, one to one relations represented in the tests I added in this PR shows the issue. I don't understand why we don't have news about this. My proposed fix may be low level and dangerous, but at least regression tests are here. Unless I'm missing something, this is a huge regression, no ? So I'm happy to discuss this, even though I'm personnaly ok with this issue since I found it and managed to deal with it by founding the hard way that it broke my projects.

@beberlei

beberlei commented Jul 5, 2026

Copy link
Copy Markdown
Member

Did not have the time to look at this yet, sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unit of work doesn't properly track changes when using native lazy object

3 participants