From 27483495e170e5af1e36eecf250e91de4903d294 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 18 Nov 2024 13:08:43 +0100 Subject: [PATCH] Fix handling of unmapped properties in proxy objects (#2698) --- .../Proxy/Factory/StaticProxyFactory.php | 14 +++++++++++--- .../Proxy/Factory/StaticProxyFactoryTest.php | 19 +++++++++++++------ .../DocumentWithUnmappedProperties.php | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 tests/Documents/DocumentWithUnmappedProperties.php diff --git a/lib/Doctrine/ODM/MongoDB/Proxy/Factory/StaticProxyFactory.php b/lib/Doctrine/ODM/MongoDB/Proxy/Factory/StaticProxyFactory.php index 737795fe48..5d7afba233 100644 --- a/lib/Doctrine/ODM/MongoDB/Proxy/Factory/StaticProxyFactory.php +++ b/lib/Doctrine/ODM/MongoDB/Proxy/Factory/StaticProxyFactory.php @@ -145,13 +145,21 @@ private function createInitializer( */ private function skippedFieldsFqns(ClassMetadata $metadata): array { - $idFieldFqcns = []; + $skippedFieldsFqns = []; foreach ($metadata->getIdentifierFieldNames() as $idField) { - $idFieldFqcns[] = $this->propertyFqcn($metadata->getReflectionProperty($idField)); + $skippedFieldsFqns[] = $this->propertyFqcn($metadata->getReflectionProperty($idField)); } - return $idFieldFqcns; + foreach ($metadata->getReflectionClass()->getProperties() as $property) { + if ($metadata->hasField($property->getName())) { + continue; + } + + $skippedFieldsFqns[] = $this->propertyFqcn($property); + } + + return $skippedFieldsFqns; } private function propertyFqcn(ReflectionProperty $property): string diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Proxy/Factory/StaticProxyFactoryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Proxy/Factory/StaticProxyFactoryTest.php index 2be5e3fc1a..5a1db88dc5 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Proxy/Factory/StaticProxyFactoryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Proxy/Factory/StaticProxyFactoryTest.php @@ -11,6 +11,7 @@ use Doctrine\ODM\MongoDB\LockException; use Doctrine\ODM\MongoDB\Tests\BaseTestCase; use Documents\Cart; +use Documents\DocumentWithUnmappedProperties; use MongoDB\Client; use MongoDB\Collection; use MongoDB\Database; @@ -22,15 +23,10 @@ class StaticProxyFactoryTest extends BaseTestCase /** @var Client|MockObject */ private Client $client; - public function setUp(): void + public function testProxyInitializeWithException(): void { - parent::setUp(); - $this->dm = $this->createMockedDocumentManager(); - } - public function testProxyInitializeWithException(): void - { $collection = $this->createMock(Collection::class); $database = $this->createMock(Database::class); @@ -81,6 +77,17 @@ private function createMockedDocumentManager(): DocumentManager return DocumentManager::create($this->client, $config); } + + public function testCreateProxyForDocumentWithUnmappedProperties(): void + { + $proxy = $this->dm->getReference(DocumentWithUnmappedProperties::class, '123'); + self::assertInstanceOf(GhostObjectInterface::class, $proxy); + + // Disable initialiser so we can access properties without initialising the object + $proxy->setProxyInitializer(null); + + self::assertSame('bar', $proxy->foo); + } } class DocumentNotFoundListener diff --git a/tests/Documents/DocumentWithUnmappedProperties.php b/tests/Documents/DocumentWithUnmappedProperties.php new file mode 100644 index 0000000000..06339e79b2 --- /dev/null +++ b/tests/Documents/DocumentWithUnmappedProperties.php @@ -0,0 +1,17 @@ +