Skip to content

Commit

Permalink
Fix handling of unmapped properties in proxy objects (#2698)
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus authored Nov 18, 2024
1 parent e4c971f commit 2748349
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
14 changes: 11 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Proxy/Factory/StaticProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/Documents/DocumentWithUnmappedProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Documents;

use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;

#[Document]
class DocumentWithUnmappedProperties
{
#[Id]
public string $id;

public string $foo = 'bar';
}

0 comments on commit 2748349

Please sign in to comment.