Skip to content

Commit

Permalink
Added nullable reverseField
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaplet committed Apr 10, 2024
1 parent 7a11aa1 commit 44ae9cf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
40 changes: 26 additions & 14 deletions src/Collection/Mapping/ArrayToEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
18 changes: 1 addition & 17 deletions src/Collection/RecipeDbSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* maxLength: int|null,
* defaultValue: mixed,
* reverseEntity: class-string,
* reverseField: string,
* reverseField: string|null,
* }
*/
class RecipeDbSchema
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
14 changes: 13 additions & 1 deletion src/Database/Entity/Auth/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
];
}
}

0 comments on commit 44ae9cf

Please sign in to comment.