Skip to content

Commit ed636b7

Browse files
authored
Merge pull request #12297 from soyuka/test
Initialize lazy ghost objects before persist
2 parents 0c8ce85 + 9bcea47 commit ed636b7

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/UnitOfWork.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
use function sprintf;
7878
use function strtolower;
7979

80+
use const PHP_VERSION_ID;
81+
8082
/**
8183
* The UnitOfWork is responsible for tracking changes to objects during an
8284
* "object-level" transaction and for writing out changes to the database
@@ -500,6 +502,10 @@ private function computeScheduleInsertsChangeSets(): void
500502
foreach ($this->entityInsertions as $entity) {
501503
$class = $this->em->getClassMetadata($entity::class);
502504

505+
if (PHP_VERSION_ID >= 80400 && $class->reflClass->isUninitializedLazyObject($entity)) {
506+
$class->reflClass->initializeLazyObject($entity);
507+
}
508+
503509
$this->computeChangeSet($class, $entity);
504510
}
505511
}

tests/Tests/ORM/Functional/StandardEntityPersisterTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
namespace Doctrine\Tests\ORM\Functional;
66

77
use Doctrine\ORM\PersistentCollection;
8+
use Doctrine\Tests\Models\CMS\CmsUser;
89
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
910
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
1011
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
1112
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
1213
use Doctrine\Tests\OrmFunctionalTestCase;
14+
use ReflectionClass;
15+
16+
use const PHP_VERSION_ID;
1317

1418
/**
1519
* Tests capabilities of the persister.
@@ -19,6 +23,7 @@ class StandardEntityPersisterTest extends OrmFunctionalTestCase
1923
protected function setUp(): void
2024
{
2125
$this->useModelSet('ecommerce');
26+
$this->useModelSet('cms');
2227

2328
parent::setUp();
2429
}
@@ -107,4 +112,29 @@ public function testAddPersistRetrieve(): void
107112
// Persisted Product now must have 3 Feature items
108113
self::assertCount(3, $res[0]->getFeatures());
109114
}
115+
116+
public function testPersistLazyGhost(): void
117+
{
118+
if (PHP_VERSION_ID < 80400) {
119+
$this->markTestSkipped('Lazy objects are only available in PHP 8.4+.');
120+
}
121+
122+
$initialized = false;
123+
$reflector = new ReflectionClass(CmsUser::class);
124+
$lazyGhost = $reflector->newLazyGhost(static function (CmsUser $object) use (&$initialized): void {
125+
$initialized = true;
126+
$object->username = 'lazyGhost';
127+
$object->name = 'LazyGhostInitialized';
128+
$object->status = 'active';
129+
});
130+
131+
self::assertFalse($initialized, 'Lazy ghost should not be initialized before persist.');
132+
$this->_em->persist($lazyGhost);
133+
$this->_em->flush();
134+
self::assertTrue($initialized, 'Lazy ghost should be initialized during flush.');
135+
$this->_em->clear();
136+
$retrievedEntity = $this->_em->find(CmsUser::class, $lazyGhost->id);
137+
self::assertNotNull($retrievedEntity);
138+
self::assertEquals('LazyGhostInitialized', $retrievedEntity->name);
139+
}
110140
}

0 commit comments

Comments
 (0)