From d9023e11e6a56737da88092c61b0f17745c06646 Mon Sep 17 00:00:00 2001 From: Mathieu Piot Date: Fri, 24 Apr 2026 12:21:07 +0200 Subject: [PATCH] [WithoutPersisting] Applied on inverseRelation if mentioned --- src/FactoryCollection.php | 8 ++++++++ src/Persistence/PersistentObjectFactory.php | 13 ++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/FactoryCollection.php b/src/FactoryCollection.php index 7754d5bca..36a73e616 100644 --- a/src/FactoryCollection.php +++ b/src/FactoryCollection.php @@ -51,6 +51,14 @@ public function withPersistMode(PersistMode $persistMode): static return $clone; } + /** + * @internal + */ + public function isPersisting(): bool + { + return $this->persistMode->isPersisting(); + } + /** * @internal * @return self diff --git a/src/Persistence/PersistentObjectFactory.php b/src/Persistence/PersistentObjectFactory.php index aa5efc870..b0e6fb995 100644 --- a/src/Persistence/PersistentObjectFactory.php +++ b/src/Persistence/PersistentObjectFactory.php @@ -372,8 +372,15 @@ protected function normalizeParameter(string $field, mixed $value): mixed } if ($value instanceof self) { + // TODO: Not know why, break some tests: + // $value = $value + // ->withPersistMode($value->persistMode()) + // ->notRootFactory(); + // So, store it instead + $isPersistingValue = $value->isPersisting(); + $value = $value - ->withPersistMode($this->persist) + ->withPersistMode($this->persistMode()) ->notRootFactory(); if (null !== $this->disabledDoctrineEventClasses) { @@ -391,7 +398,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed $value = $value ->reuse(...$this->reusedObjects(), ...$value->reusedObjects()) ->withPersistMode( - $this->isPersisting() ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING + $this->isPersisting() && $isPersistingValue ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING ) ; @@ -444,7 +451,7 @@ protected function normalizeCollection(string $field, FactoryCollection $collect $inverseObjects = $collection ->reuse(...$this->reusedObjects()) - ->withPersistMode($this->isPersisting() ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING) + ->withPersistMode($this->isPersisting() && $collection->isPersisting() ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING) ->create([$inverseField => $object]); $inverseObjects = ProxyGenerator::unwrap($inverseObjects, withAutoRefresh: false);