Skip to content

Commit

Permalink
fix: can create inversed one to one with non nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Nov 29, 2024
1 parent 8cbee90 commit ea1a2f3
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Zenstruck\Foundry\Persistence\Exception\NotEnoughObjects;
use Zenstruck\Foundry\Persistence\Exception\RefreshObjectFailed;

use function Zenstruck\Foundry\get;

/**
* @author Kevin Bond <[email protected]>
*
Expand Down Expand Up @@ -271,13 +273,20 @@ protected function normalizeParameter(string $field, mixed $value): mixed

// handle inversed OneToOne
if ($relationshipMetadata && !$relationshipMetadata->isCollection && $inverseField = $relationshipMetadata->inverseField) {
$this->tempAfterPersist[] = static function(object $object) use ($value, $inverseField, $pm) {
$inversedObject = unproxy($value->create());
$this->tempAfterPersist[] = static function(object $object) use ($value, $inverseField, $pm, $inversedObject, $field) {

Check failure on line 277 in src/Persistence/PersistentObjectFactory.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Anonymous function has an unused use $field.
$value->create([$inverseField => $object]);
$pm->refresh($object);
$oldObj = get($inversedObject, $inverseField);
delete($inversedObject);
if ($oldObj) {
delete($oldObj);

Check failure on line 283 in src/Persistence/PersistentObjectFactory.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Unable to resolve the template type T in call to function Zenstruck\Foundry\Persistence\delete
}
};

// creation delegated to afterPersist hook - return empty array here
return null;
// let's already add the current inversedObject,
// but we'll need to remove all its potential "owning side entity" afterward (see the after persist closure)
return $inversedObject;
}

if (Configuration::instance()->persistence()->relationshipMetadata(static::class(), $value::class(), $field)?->isCascadePersist) {
Expand Down

0 comments on commit ea1a2f3

Please sign in to comment.