diff --git a/src/Persistence/PersistenceManager.php b/src/Persistence/PersistenceManager.php index 70f6f985..ef095f20 100644 --- a/src/Persistence/PersistenceManager.php +++ b/src/Persistence/PersistenceManager.php @@ -186,6 +186,29 @@ public function refresh(object &$object, bool $force = false): object return $object; } + /** + * @template T of object + * + * @param T $object + * + * @return ?T + */ + public function findPersisted(object $object): ?object + { + if ($object instanceof Proxy) { + $object = unproxy($object); + } + + $om = $this->strategyFor($object::class)->objectManagerFor($object::class); + $id = $om->getClassMetadata($object::class)->getIdentifierValues($object); + + if (!$id) { + return null; + } + + return $om->find($object::class, $id); + } + public function isPersisted(object $object): bool { if ($object instanceof Proxy) { diff --git a/src/Persistence/PersistentObjectFactory.php b/src/Persistence/PersistentObjectFactory.php index 1167a95a..b32abca7 100644 --- a/src/Persistence/PersistentObjectFactory.php +++ b/src/Persistence/PersistentObjectFactory.php @@ -359,11 +359,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