Skip to content

Skip setting initialized readonly properties during refresh (#9505)#12496

Open
YoussefMansour9 wants to merge 2 commits into
doctrine:3.6.xfrom
YoussefMansour9:fix/readonly-property-refresh
Open

Skip setting initialized readonly properties during refresh (#9505)#12496
YoussefMansour9 wants to merge 2 commits into
doctrine:3.6.xfrom
YoussefMansour9:fix/readonly-property-refresh

Conversation

@YoussefMansour9

Copy link
Copy Markdown

Fixes #9505

Problem

$em->refresh($entity) crashes with LogicException when entity has readonly properties typed as objects (e.g., DateTimeImmutable). During hydration, ReadonlyAccessor::setValue() compares old and new values by identity (!==). Different PHP object instances of the same logical value trigger the comparison and throw.

Fix

In UnitOfWork::createEntity(), skip calling setValue() for fields whose accessor is ReadonlyAccessor and whose property is already initialized on the entity during refresh.

Testing

  • All 1591 functional tests pass (0 failures)
  • All 40 UnitOfWork unit tests pass
  • All 7 Readonly functional tests pass

@greg0ire

greg0ire commented Jun 3, 2026

Copy link
Copy Markdown
Member

Testing

  • All 1591 functional tests pass (0 failures)
  • All 40 UnitOfWork unit tests pass
  • All 7 Readonly functional tests pass

That's cool and all, but you should still add a test that would fail without your patch.

Comment thread src/UnitOfWork.php
&& $accessor instanceof ReadonlyAccessor
&& $accessor->getUnderlyingReflector()->isInitialized($entity)
) {
continue;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤔 if you just skip the initialization, and the date has been changed by another process, refresh just won't work, leading to bugs.

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.

Bug: UnitOfWork attempts to update readonly property (object IDs don't match)

2 participants