Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong initialisation of proxies with public properties #2082

Merged
merged 1 commit into from
Oct 9, 2019
Merged
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
15 changes: 13 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,19 @@ public function hydrate(object $document, array $data, array $hints = []) : arra
}
}

if ($document instanceof GhostObjectInterface) {
$document->setProxyInitializer(null);
if ($document instanceof GhostObjectInterface && $document->getProxyInitializer() !== null) {
// Inject an empty initialiser to not load any object data
$document->setProxyInitializer(static function (
GhostObjectInterface $ghostObject,
string $method, // we don't care
array $parameters, // we don't care
&$initializer,
array $properties // we currently do not use this
) : bool {
$initializer = null;

return true;
});
}

$data = $this->getHydratorFor($metadata->name)->hydrate($document, $data, $hints);
Expand Down
23 changes: 23 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,37 @@ public function testHydrator()
$this->assertEquals('jon', $user->name);
$this->assertInstanceOf(DateTime::class, $user->birthdate);
$this->assertInstanceOf(HydrationClosureReferenceOne::class, $user->referenceOne);
$this->assertInstanceOf(GhostObjectInterface::class, $user->referenceOne);
$this->assertInstanceOf(PersistentCollection::class, $user->referenceMany);
$this->assertInstanceOf(GhostObjectInterface::class, $user->referenceMany[0]);
$this->assertInstanceOf(GhostObjectInterface::class, $user->referenceMany[1]);
$this->assertInstanceOf(HydrationClosureEmbedOne::class, $user->embedOne);
$this->assertInstanceOf(PersistentCollection::class, $user->embedMany);
$this->assertEquals('jon', $user->embedOne->name);
$this->assertEquals('jon', $user->embedMany[0]->name);
}

public function testHydrateProxyWithMissingAssociations()
alcaeus marked this conversation as resolved.
Show resolved Hide resolved
{
$user = $this->dm->getReference(HydrationClosureUser::class, 1);
$this->assertInstanceOf(GhostObjectInterface::class, $user);

$this->dm->getHydratorFactory()->hydrate($user, [
'_id' => 1,
'title' => null,
'name' => 'jon',
]);

$this->assertEquals(1, $user->id);
$this->assertNull($user->title);
$this->assertEquals('jon', $user->name);
$this->assertNull($user->birthdate);
$this->assertNull($user->referenceOne);
$this->assertInstanceOf(PersistentCollection::class, $user->referenceMany);
$this->assertNull($user->embedOne);
$this->assertInstanceOf(PersistentCollection::class, $user->embedMany);
}

public function testReadOnly()
{
$class = $this->dm->getClassMetadata(HydrationClosureUser::class);
Expand Down