From 4a9101f383e9f5d19f7d65cc6d4ad3b77c031f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20Bret=C3=A9ch=C3=A9?= Date: Mon, 16 Dec 2024 17:20:46 +0100 Subject: [PATCH] Check hint value before considering instance read-only This fixes a bug that occurs when calling setHint(Query::HINT_READ_ONLY, false) from a query object. UnitOfWork checks if this hint exists without considering the value passed as second argument. Handling the second parameter improves consistency with documentation. https://www.doctrine-project.org/projects/doctrine-orm/en/2.20/reference/improving-performance.html#read-only-entities --- src/UnitOfWork.php | 2 +- tests/Tests/ORM/Functional/ReadOnlyTest.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index 73aae2f362e..ff30f14f6d8 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -2968,7 +2968,7 @@ public function createEntity($className, array $data, &$hints = []) $oid = spl_object_id($entity); $this->registerManaged($entity, $id, $data); - if (isset($hints[Query::HINT_READ_ONLY])) { + if (isset($hints[Query::HINT_READ_ONLY]) && $hints[Query::HINT_READ_ONLY] === true) { $this->readOnlyObjects[$oid] = true; } } diff --git a/tests/Tests/ORM/Functional/ReadOnlyTest.php b/tests/Tests/ORM/Functional/ReadOnlyTest.php index 4d06e107d07..297c2d5a3e4 100644 --- a/tests/Tests/ORM/Functional/ReadOnlyTest.php +++ b/tests/Tests/ORM/Functional/ReadOnlyTest.php @@ -90,6 +90,24 @@ public function testReadOnlyQueryHint(): void self::assertTrue($this->_em->getUnitOfWork()->isReadOnly($user)); } + public function testNotReadOnlyQueryHint(): void + { + $user = new ReadOnlyEntity('beberlei', 1234); + + $this->_em->persist($user); + + $this->_em->flush(); + $this->_em->clear(); + + $query = $this->_em->createQuery('SELECT u FROM ' . ReadOnlyEntity::class . ' u WHERE u.id = ?1'); + $query->setParameter(1, $user->id); + $query->setHint(Query::HINT_READ_ONLY, false); + + $user = $query->getSingleResult(); + + self::assertFalse($this->_em->getUnitOfWork()->isReadOnly($user)); + } + public function testNotReadOnlyIfObjectWasProxyBefore(): void { $user = new ReadOnlyEntity('beberlei', 1234);