From c2a5ea9c5b9c6a44b10cb44d3df520bab9dcf687 Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Thu, 2 Jan 2025 18:27:31 +0100 Subject: [PATCH] fix: remove _refresh call from create object process --- src/Persistence/PersistenceManager.php | 24 ++++++++++++++++++--- src/Persistence/PersistentObjectFactory.php | 8 +------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Persistence/PersistenceManager.php b/src/Persistence/PersistenceManager.php index 70f6f985..70872a4b 100644 --- a/src/Persistence/PersistenceManager.php +++ b/src/Persistence/PersistenceManager.php @@ -186,7 +186,14 @@ public function refresh(object &$object, bool $force = false): object return $object; } - public function isPersisted(object $object): bool + /** + * @template T of object + * + * @param T $object + * + * @return ?T + */ + public function findPersisted(object $object): ?object { if ($object instanceof Proxy) { $object = unproxy($object); @@ -195,7 +202,16 @@ public function isPersisted(object $object): bool $om = $this->strategyFor($object::class)->objectManagerFor($object::class); $id = $om->getClassMetadata($object::class)->getIdentifierValues($object); - return $id && null !== $om->find($object::class, $id); + if (!$id) { + return null; + } + + return $om->find($object::class, $id); + } + + public function isPersisted(object $object): bool + { + return (bool) $this->findPersisted($object); } /** @@ -318,7 +334,9 @@ public function embeddablePropertiesFor(object $object, string $owner): ?array public function hasPersistenceFor(object $object): bool { try { - return (bool) $this->strategyFor($object::class); + $strategy = $this->strategyFor($object::class); + + return !$strategy->isEmbeddable($object); } catch (NoPersistenceStrategy) { return false; } diff --git a/src/Persistence/PersistentObjectFactory.php b/src/Persistence/PersistentObjectFactory.php index 1167a95a..d6a6ec19 100644 --- a/src/Persistence/PersistentObjectFactory.php +++ b/src/Persistence/PersistentObjectFactory.php @@ -12,7 +12,6 @@ namespace Zenstruck\Foundry\Persistence; use Doctrine\Persistence\ObjectRepository; -use Symfony\Component\VarExporter\Exception\LogicException as VarExportLogicException; use Zenstruck\Foundry\Configuration; use Zenstruck\Foundry\Exception\PersistenceDisabled; use Zenstruck\Foundry\Exception\PersistenceNotAvailable; @@ -20,7 +19,6 @@ use Zenstruck\Foundry\FactoryCollection; use Zenstruck\Foundry\ObjectFactory; use Zenstruck\Foundry\Persistence\Exception\NotEnoughObjects; -use Zenstruck\Foundry\Persistence\Exception\RefreshObjectFailed; use function Zenstruck\Foundry\set; @@ -359,11 +357,7 @@ protected function normalizeObject(object $object): object return $object; } - try { - return proxy($object)->_refresh()->_real(); - } catch (RefreshObjectFailed|VarExportLogicException) { - return $object; - } + return $configuration->persistence()->findPersisted($object) ?? $object; } final protected function isPersisting(): bool