Skip to content

Commit 2f523df

Browse files
authored
fix: use Doctrine metadata event when persist is disabled (#841)
1 parent c28b9d7 commit 2f523df

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/Persistence/PersistentObjectFactory.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,15 @@ protected function normalizeParameter(string $field, mixed $value): mixed
291291
if ($inversedRelationshipMetadata && !$inversedRelationshipMetadata->isCollection) {
292292
$inverseField = $inversedRelationshipMetadata->inverseField;
293293

294-
// we need to handle the circular dependency involved by inversed one-to-one relationship:
295-
// a placeholder object is used, which will be replaced by the real object, after its instantiation
296-
$inversedObject = $value->withPersistMode(PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT)
297-
->create([$inverseField => $placeholder = (new \ReflectionClass(static::class()))->newInstanceWithoutConstructor()]);
294+
$inversedObject = $value->withPersistMode(
295+
$this->isPersisting() ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING
296+
)
297+
298+
// we need to handle the circular dependency involved by inversed one-to-one relationship:
299+
// a placeholder object is used, which will be replaced by the real object, after its instantiation
300+
->create([
301+
$inverseField => $placeholder = (new \ReflectionClass(static::class()))->newInstanceWithoutConstructor(),
302+
]);
298303

299304
$inversedObject = unproxy($inversedObject, withAutoRefresh: false);
300305

@@ -312,7 +317,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed
312317

313318
protected function normalizeCollection(string $field, FactoryCollection $collection): array
314319
{
315-
if (!$this->isPersisting() || !$collection->factory instanceof self) {
320+
if (!Configuration::instance()->isPersistenceAvailable() || !$collection->factory instanceof self) {
316321
return parent::normalizeCollection($field, $collection);
317322
}
318323

@@ -321,10 +326,13 @@ protected function normalizeCollection(string $field, FactoryCollection $collect
321326
$inverseRelationshipMetadata = $pm->inverseRelationshipMetadata(static::class(), $collection->factory::class(), $field);
322327

323328
if ($inverseRelationshipMetadata && $inverseRelationshipMetadata->isCollection) {
324-
$this->tempAfterInstantiate[] = static function(object $object) use ($collection, $inverseRelationshipMetadata, $field) {
329+
$this->tempAfterInstantiate[] = function(object $object) use ($collection, $inverseRelationshipMetadata, $field) {
325330
$inverseField = $inverseRelationshipMetadata->inverseField;
326331

327-
$inverseObjects = $collection->withPersistMode(PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT)->create([$inverseField => $object]);
332+
$inverseObjects = $collection->withPersistMode(
333+
$this->isPersisting() ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING
334+
)
335+
->create([$inverseField => $object]);
328336

329337
$inverseObjects = unproxy($inverseObjects, withAutoRefresh: false);
330338

tests/Integration/ORM/EntityRelationship/EntityFactoryRelationshipTestCase.php

+31
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,21 @@ public function disabling_persistence_cascades_to_children(): void
324324
foreach ($contact->getTags() as $tag) {
325325
$this->assertNull($tag->id);
326326
}
327+
}
327328

329+
/** @test */
330+
#[Test]
331+
#[DataProvider('provideCascadeRelationshipsCombinations')]
332+
#[UsingRelationships(Contact::class, ['category'])]
333+
public function disabling_persistence_cascades_to_children_one_to_many(): void
334+
{
328335
$category = static::categoryFactory()->withoutPersisting()->create([
329336
'contacts' => static::contactFactory()->many(3),
330337
]);
331338

339+
// ensure nothing was persisted in Doctrine by flushing
340+
self::getContainer()->get(EntityManagerInterface::class)->flush(); // @phpstan-ignore method.notFound
341+
332342
static::contactFactory()::assert()->empty();
333343
static::categoryFactory()::assert()->empty();
334344

@@ -340,6 +350,27 @@ public function disabling_persistence_cascades_to_children(): void
340350
}
341351
}
342352

353+
/** @test */
354+
#[Test]
355+
#[DataProvider('provideCascadeRelationshipsCombinations')]
356+
#[UsingRelationships(Contact::class, ['address'])]
357+
public function disabling_persistence_cascades_to_children_inversed_one_to_one(): void
358+
{
359+
$address = static::addressFactory()->withoutPersisting()->create([
360+
'contact' => static::contactFactory(),
361+
]);
362+
363+
// ensure nothing was persisted in Doctrine by flushing
364+
self::getContainer()->get(EntityManagerInterface::class)->flush(); // @phpstan-ignore method.notFound
365+
366+
static::contactFactory()::assert()->empty();
367+
static::addressFactory()::assert()->empty();
368+
369+
$this->assertNull($address->id);
370+
$this->assertInstanceOf(Contact::class, $address->getContact());
371+
$this->assertNull($address->getContact()->id);
372+
}
373+
343374
/** @test */
344375
#[Test]
345376
#[DataProvider('provideCascadeRelationshipsCombinations')]

0 commit comments

Comments
 (0)