Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,18 @@ public function createEntity(string $className, array $data, array &$hints = [])

foreach ($data as $field => $value) {
if (isset($class->fieldMappings[$field])) {
$class->propertyAccessors[$field]->setValue($entity, $value);
$accessor = $class->propertyAccessors[$field];

// During refresh, skip already-initialized readonly properties.
if (
isset($hints[Query::HINT_REFRESH])
&& $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.

}

$accessor->setValue($entity, $value);
}
}

Expand Down
46 changes: 46 additions & 0 deletions tests/Tests/ORM/Functional/GH9505Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;

class GH9505Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->createSchemaForModels(GH9505ReadonlyDateEntity::class);
}

public function testRefreshDoesNotThrowOnReadonlyObjectProperty(): void
{
$entity = new GH9505ReadonlyDateEntity(new DateTimeImmutable('2022-01-01'));

$this->_em->persist($entity);
$this->_em->flush();

$this->_em->refresh($entity);

$this->assertSame('2022-01-01', $entity->date->format('Y-m-d'));
}
}

#[ORM\Entity]
class GH9505ReadonlyDateEntity
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
public readonly int $id;

public function __construct(
#[ORM\Column(type: 'datetime_immutable', nullable: false)]
public readonly DateTimeImmutable $date,
) {
}
}
Loading