From 44ae9cf9585453a0ad6935a9c505c4467c73e5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Zapletal?= Date: Tue, 9 Apr 2024 21:03:11 -0600 Subject: [PATCH] Added nullable reverseField --- src/Collection/Mapping/ArrayToEntity.php | 40 +++++++++++++++--------- src/Collection/RecipeDbSchema.php | 18 +---------- src/Database/Entity/Auth/Role.php | 14 ++++++++- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/Collection/Mapping/ArrayToEntity.php b/src/Collection/Mapping/ArrayToEntity.php index 9e18926..cdb4f78 100644 --- a/src/Collection/Mapping/ArrayToEntity.php +++ b/src/Collection/Mapping/ArrayToEntity.php @@ -87,12 +87,16 @@ protected static function resolveOneToOneReverseRelation(string $fieldKey, Recip if ($currentValue !== null) { $targetRef = new \ReflectionClass($currentValue); $targetField = $schemas[array_search($fieldKey, $schemaNames)]['reverseField']; - $targetRef->getProperty($targetField)->setValue($currentValue, null); + if ($targetField !== null) { + $targetRef->getProperty($targetField)->setValue($currentValue, null); + } self::addEntityToFlush($currentValue); } if ($value !== null) { - $ref->getProperty($colSchema['reverseField'])->setValue($value, $current); + if ($colSchema['reverseField'] !== null) { + $ref->getProperty($colSchema['reverseField'])->setValue($value, $current); + } self::addEntityToFlush($value); } } @@ -111,16 +115,20 @@ protected static function resolveOneToManyReverseRelation(string $fieldKey, Reci foreach ($currentItems as $item) { if (!$value->contains($item)) { $currentItems->removeElement($item); - $itemRef = new \ReflectionClass($item); - $itemRef->getProperty($colSchema['reverseField'])->setValue($item, null); + if ($colSchema['reverseField'] !== null) { + $itemRef = new \ReflectionClass($item); + $itemRef->getProperty($colSchema['reverseField'])->setValue($item, null); + } self::addEntityToFlush($item); } } foreach ($value as $item) { $colSchema = $schemas[array_search($fieldKey, $schemaNames)]; - $currentRef = new \ReflectionClass($colSchema['reverseEntity']); - $currentRef->getProperty($colSchema['reverseField'])->setValue($item, $current); + if ($colSchema['reverseField'] !== null) { + $currentRef = new \ReflectionClass($colSchema['reverseEntity']); + $currentRef->getProperty($colSchema['reverseField'])->setValue($item, $current); + } self::addEntityToFlush($item); } } @@ -138,19 +146,23 @@ protected static function resolveManyToManyReverseRelation(string $fieldKey, Rec $currentItems = $currentProp->getValue($current); foreach ($value as $item) { - $itemRef = new \ReflectionClass($item); - $collection = $itemRef->getProperty($colSchema['reverseField'])->getValue($item); - if (!$currentItems->contains($current) && !$collection->contains($current) ) { - $collection->add($current); - self::addEntityToFlush($item); + if ($colSchema['reverseField'] !== null) { + $itemRef = new \ReflectionClass($item); + $collection = $itemRef->getProperty($colSchema['reverseField'])->getValue($item); + if (!$currentItems->contains($current) && !$collection->contains($current)) { + $collection->add($current); + self::addEntityToFlush($item); + } } } foreach ($currentItems as $item) { if (!$value->contains($item)) { - $itemRef = new \ReflectionClass($item); - $collection = $itemRef->getProperty($colSchema['reverseField'])->getValue($item); - $collection->removeElement($current); + if ($colSchema['reverseField'] !== null) { + $itemRef = new \ReflectionClass($item); + $collection = $itemRef->getProperty($colSchema['reverseField'])->getValue($item); + $collection->removeElement($current); + } self::addEntityToFlush($item); } } diff --git a/src/Collection/RecipeDbSchema.php b/src/Collection/RecipeDbSchema.php index 118b276..de8e554 100644 --- a/src/Collection/RecipeDbSchema.php +++ b/src/Collection/RecipeDbSchema.php @@ -31,7 +31,7 @@ * maxLength: int|null, * defaultValue: mixed, * reverseEntity: class-string, - * reverseField: string, + * reverseField: string|null, * } */ class RecipeDbSchema @@ -93,10 +93,6 @@ public function addOneToOneColumn(OneToOne $attr, \ReflectionProperty $prop): vo $reverseField = $attr->inversedBy; } - if ($reverseField === null) { - throw new \InvalidArgumentException('Attribute mappedBy or inversedBy is required'); - } - $this->oneToOneColumns[] = [ 'name' => $prop->getName(), 'type' => 'one_to_one', @@ -115,10 +111,6 @@ public function addOneToManyColumn(OneToMany $attr, \ReflectionProperty $prop): throw new \InvalidArgumentException('Attribute targetEntity is required'); } - if ($attr->mappedBy === null) { - throw new \InvalidArgumentException('Attribute mappedBy is required'); - } - $this->oneToManyColumns[] = [ 'name' => $prop->getName(), 'type' => 'one_to_many', @@ -137,10 +129,6 @@ public function addManyToOneColumn(ManyToOne $attr, \ReflectionProperty $prop): throw new \InvalidArgumentException('Attribute targetEntity is required'); } - if ($attr->inversedBy === null) { - throw new \InvalidArgumentException('Attribute inversedBy is required'); - } - $this->manyToOneColumns[] = [ 'name' => $prop->getName(), 'type' => 'many_to_one', @@ -164,10 +152,6 @@ public function addManyToManyColumn(ManyToMany $attr, \ReflectionProperty $prop) $reverseField = $attr->inversedBy; } - if ($reverseField === null) { - throw new \InvalidArgumentException('Attribute mappedBy or inversedBy is required'); - } - $this->manyToManyColumns[] = [ 'name' => $prop->getName(), 'type' => 'many_to_many', diff --git a/src/Database/Entity/Auth/Role.php b/src/Database/Entity/Auth/Role.php index e7ea407..750d491 100644 --- a/src/Database/Entity/Auth/Role.php +++ b/src/Database/Entity/Auth/Role.php @@ -10,12 +10,13 @@ use Megio\Database\Entity\EntityException; use Megio\Database\Field\TCreatedAt; use Megio\Database\Field\TId; +use Megio\Database\Interface\IJoinable; use Megio\Database\Repository\Auth\RoleRepository; #[ORM\Table(name: '`auth_role`')] #[ORM\Entity(repositoryClass: RoleRepository::class)] #[ORM\HasLifecycleCallbacks] -class Role +class Role implements IJoinable { use TId, TCreatedAt; @@ -80,4 +81,15 @@ public function preventAdminRole(): void throw new EntityException('You can not create admin role, admin role is default.'); } } + + /** + * @return array{fields: string[], format: string} + */ + public function getJoinableLabel(): array + { + return [ + 'fields' => ['name'], + 'format' => '%s', + ]; + } } \ No newline at end of file