- Allowed a JOINed entity to be null if nothing is loaded from a query and it's field is nullable

This commit is contained in:
Dave Mc Nicoll 2024-10-14 16:32:23 +00:00
parent 2fda7e82d7
commit 5f4f23a8e4
1 changed files with 8 additions and 6 deletions

View File

@ -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();
}
}