Skip to content

Commit

Permalink
fix: remove _refresh call from create object process (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored Jan 2, 2025
1 parent 5a93ba5 commit f303f3f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 21 additions & 3 deletions src/Persistence/PersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 1 addition & 7 deletions src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
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;
use Zenstruck\Foundry\Factory;
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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f303f3f

Please sign in to comment.