diff --git a/src/Repository/RelationBuilder.php b/src/Repository/RelationBuilder.php index 1463920..3d368a3 100644 --- a/src/Repository/RelationBuilder.php +++ b/src/Repository/RelationBuilder.php @@ -3,7 +3,7 @@ namespace Ulmus\Repository; use Ulmus\{ - Ulmus, Query, Common\EntityResolver, Repository, Event, + Ulmus, Query, Common\EntityResolver, Repository, Event, Common\Sql }; use Ulmus\Attribute\Property\{ @@ -91,7 +91,7 @@ class RelationBuilder 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->wheres = $this->resolver->searchFieldAnnotationList($name, [ Where::class ] ); $this->filters = $this->resolver->searchFieldAnnotationList($name, [ Filter::class ] ); @@ -276,11 +276,16 @@ class RelationBuilder if ( $relation->generateKey ) { $value = call_user_func_array($relation->generateKey, [ $this->entity ]); } - else { + elseif (isset($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); @@ -340,7 +345,7 @@ class RelationBuilder return []; } - + protected function fieldIsNullable(string $name) : bool { return $this->resolver->reflectedClass->getProperties(true)[$name]->allowsNull();