- Fixed RelationBuilder problem whenever a field wasn't set

This commit is contained in:
Dave Mc Nicoll 2025-05-15 18:09:52 +00:00
parent cc6048d4ee
commit 8fff21ecb0

View File

@ -3,7 +3,7 @@
namespace Ulmus\Repository; namespace Ulmus\Repository;
use Ulmus\{ use Ulmus\{
Ulmus, Query, Common\EntityResolver, Repository, Event, Ulmus, Query, Common\EntityResolver, Repository, Event, Common\Sql
}; };
use Ulmus\Attribute\Property\{ use Ulmus\Attribute\Property\{
@ -91,7 +91,7 @@ class RelationBuilder
protected function resolveRelation(string $name) : mixed protected function resolveRelation(string $name) : mixed
{ {
if ( null !== ( $relation = $this->resolver->searchFieldAnnotation($name, [ Relation::class, /*Join::class*/ ] ) ) ) { if ( null !== ( $relation = $this->resolver->searchFieldAnnotation($name, [ Relation::class ] ) ) ) {
$this->orders = $this->resolver->searchFieldAnnotationList($name, [ OrderBy::class ] ); $this->orders = $this->resolver->searchFieldAnnotationList($name, [ OrderBy::class ] );
$this->wheres = $this->resolver->searchFieldAnnotationList($name, [ Where::class ] ); $this->wheres = $this->resolver->searchFieldAnnotationList($name, [ Where::class ] );
$this->filters = $this->resolver->searchFieldAnnotationList($name, [ Filter::class ] ); $this->filters = $this->resolver->searchFieldAnnotationList($name, [ Filter::class ] );
@ -276,11 +276,16 @@ class RelationBuilder
if ( $relation->generateKey ) { if ( $relation->generateKey ) {
$value = call_user_func_array($relation->generateKey, [ $this->entity ]); $value = call_user_func_array($relation->generateKey, [ $this->entity ]);
} }
else { elseif (isset($this->entity->$field)) {
$value = $this->entity->$field; $value = $this->entity->$field;
} }
$this->repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity::field($relation->foreignKey), $value ); if (isset($value)) {
$this->repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity::field($relation->foreignKey), $value );
}
else {
$this->repository->where(Sql::raw('TRUE'), Sql::raw('FALSE'));
}
} }
return $this->applyFilter($this->repository, $name); return $this->applyFilter($this->repository, $name);
@ -340,7 +345,7 @@ class RelationBuilder
return []; return [];
} }
protected function fieldIsNullable(string $name) : bool protected function fieldIsNullable(string $name) : bool
{ {
return $this->resolver->reflectedClass->getProperties(true)[$name]->allowsNull(); return $this->resolver->reflectedClass->getProperties(true)[$name]->allowsNull();