diff --git a/src/Repository/RelationBuilder.php b/src/Repository/RelationBuilder.php index 07f6c76..48f6f98 100644 --- a/src/Repository/RelationBuilder.php +++ b/src/Repository/RelationBuilder.php @@ -111,7 +111,7 @@ class RelationBuilder $value = call_user_func([ $this->repository, $relation->function() ]) ?? null; - if ($value === null && $this->resolver->reflectedClass->getProperties(true)[$name]->allowsNull()) { + if ($value === null && $this->fieldIsNullable($name) ) { return null; } @@ -203,7 +203,7 @@ class RelationBuilder throw new \InvalidArgumentException("Unknown or no relation was provided as relation type."); } - protected function fetchFromDataset($name, ? array $data = null) : object|bool + protected function fetchFromDataset($name, ? array $data = null) : object|bool|null { $attribute = $this->resolver->searchFieldAnnotation($name, [ Join::class ]) ?: $this->resolver->searchFieldAnnotation($name, [ Relation::class ]); @@ -221,10 +221,7 @@ class RelationBuilder $entity = $attribute->entity ?? $this->resolver->reflectedClass->getProperties()[$name]->getTypes()[0]->type; } - $name = strtolower($name); - foreach($data ?: $this->entity->entityLoadedDataset as $key => $value) { - if ( $key === sprintf(static::SUBQUERY_FIELD_SUFFIX, strtolower($name)) ) { if ($value) { if ( null === ( $dataset = \json_decode($value, true) ) ) { @@ -250,7 +247,7 @@ class RelationBuilder return ( new $entity() )->fromArray($data)->resetVirtualProperties(); } else { - return new $entity(); + return $this->fieldIsNullable($name) ? null : new $entity(); } } } @@ -343,4 +340,9 @@ class RelationBuilder return []; } + + protected function fieldIsNullable(string $name) : bool + { + return $this->resolver->reflectedClass->getProperties(true)[$name]->allowsNull(); + } } \ No newline at end of file