Skip to content

Commit

Permalink
Fix handling of unmapped properties in proxy objects
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Nov 15, 2024
1 parent e4c971f commit a8186c0
Show file tree
Hide file tree
Showing 3 changed files with 39 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()

Check failure on line 81 in tests/Doctrine/ODM/MongoDB/Tests/Proxy/Factory/StaticProxyFactoryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method Doctrine\ODM\MongoDB\Tests\Proxy\Factory\StaticProxyFactoryTest::testCreateProxyForDocumentWithUnmappedProperties() has no return type specified.
{
$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
15 changes: 15 additions & 0 deletions tests/Documents/DocumentWithUnmappedProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

Check failure on line 1 in tests/Documents/DocumentWithUnmappedProperties.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Missing 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 a8186c0

Please sign in to comment.