Skip to content

Commit c703491

Browse files
committed
skip identifier field in computeChangeSet
1 parent 84a4935 commit c703491

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

lib/Doctrine/ODM/MongoDB/UnitOfWork.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,11 @@ private function computeOrRecomputeChangeSet(ClassMetadata $class, object $docum
761761
continue;
762762
}
763763

764+
// skip identifier field
765+
if (in_array($propName, $class->getIdentifierFieldNames(), true)) {
766+
continue;
767+
}
768+
764769
$orgValue = $originalData[$propName] ?? null;
765770

766771
// skip if value has not changed

tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Documents\File;
2121
use Documents\FileWithoutMetadata;
2222
use Documents\ForumAvatar;
23+
use Documents\ForumStar;
2324
use Documents\ForumUser;
2425
use Documents\Functional\NotSaved;
2526
use Documents\User;
@@ -497,6 +498,34 @@ public function testRecomputeChangesetForUninitializedProxyDoesNotCreateChangese
497498
self::assertEquals([], $this->uow->getDocumentChangeSet($user->getAvatar()));
498499
}
499500

501+
/**
502+
* Native lazy ghosts objects are marked as initialized when the last property
503+
* is set. It appends when the document class has only one property: the id.
504+
*/
505+
public function testRecomputeChangesetForNativeLazyGhostWithOnlyIdDoesNotCreateChangeset(): void
506+
{
507+
$user = new ForumUser();
508+
$user->username = '12345';
509+
$user->setStar(new ForumStar());
510+
511+
$this->dm->persist($user);
512+
$this->dm->flush();
513+
514+
$id = $user->getId();
515+
$this->dm->clear();
516+
517+
$user = $this->dm->find(ForumUser::class, $id);
518+
self::assertInstanceOf(ForumUser::class, $user);
519+
520+
self::assertTrue(self::isLazyObject($user->getStar()));
521+
522+
$classMetadata = $this->dm->getClassMetadata(ForumStar::class);
523+
524+
$this->uow->recomputeSingleDocumentChangeSet($classMetadata, $user->getStar());
525+
526+
self::assertEquals([], $this->uow->getDocumentChangeSet($user->getStar()));
527+
}
528+
500529
public function testCommitsInProgressIsUpdatedOnException(): void
501530
{
502531
$this->dm->getEventManager()->addEventSubscriber(

tests/Documents/ForumAvatar.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#[ODM\Document]
1010
class ForumAvatar
1111
{
12-
/** @var string|null */
1312
#[ODM\Id]
14-
public $id;
13+
public ?string $id;
14+
15+
#[ODM\Field]
16+
public ?string $url = null;
1517
}

tests/Documents/ForumStar.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Documents;
6+
7+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
8+
9+
/**
10+
* Special entity that only has the $id field.
11+
*/
12+
#[ODM\Document]
13+
class ForumStar
14+
{
15+
#[ODM\Id]
16+
public ?string $id;
17+
}

tests/Documents/ForumUser.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class ForumUser
2222
#[ODM\ReferenceOne(targetDocument: ForumAvatar::class, cascade: ['persist'])]
2323
public $avatar;
2424

25+
#[ODM\ReferenceOne(targetDocument: ForumStar::class, cascade: ['persist'])]
26+
public ?ForumStar $star;
27+
2528
/** @return int|ObjectId|null */
2629
public function getId()
2730
{
@@ -42,4 +45,14 @@ public function setAvatar(ForumAvatar $avatar): void
4245
{
4346
$this->avatar = $avatar;
4447
}
48+
49+
public function getStar(): ForumStar
50+
{
51+
return $this->star;
52+
}
53+
54+
public function setStar(ForumStar $star): void
55+
{
56+
$this->star = $star;
57+
}
4558
}

0 commit comments

Comments
 (0)