-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: can create inversed one to one with non nullable
- Loading branch information
Showing
1 changed file
with
12 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
* | ||
|
@@ -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) { | ||
$value->create([$inverseField => $object]); | ||
$pm->refresh($object); | ||
$oldObj = get($inversedObject, $inverseField); | ||
delete($inversedObject); | ||
if ($oldObj) { | ||
delete($oldObj); | ||
} | ||
}; | ||
|
||
// 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) { | ||
|