diff --git a/app/Repository/BaseRepository.php b/app/Repository/BaseRepository.php index 9e0354f..5fb0120 100644 --- a/app/Repository/BaseRepository.php +++ b/app/Repository/BaseRepository.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace App\Repository; +use App\Traits\RepositoryTrait; use Hyperf\Database\Model\Model; use Hyperf\Database\Model\Builder; use Hyperf\DbConnection\Db; @@ -183,7 +184,7 @@ abstract class BaseRepository /** * 通过 Model 读取分页数据 * - * @param Builder $model 查询 Model + * @param Builder $model 查询构造器 * @param array $fields 查询字段 * @param int $page 当前分页 * @param int $size 分页大小 diff --git a/app/Repository/RepositoryTrait.php b/app/Traits/RepositoryTrait.php similarity index 96% rename from app/Repository/RepositoryTrait.php rename to app/Traits/RepositoryTrait.php index bd8f073..3519aa5 100644 --- a/app/Repository/RepositoryTrait.php +++ b/app/Traits/RepositoryTrait.php @@ -1,7 +1,7 @@ addNewWhere($model, $item, $or, $field); + $this->addNewWhere($model, $item, $or); continue; } @@ -229,15 +230,15 @@ trait RepositoryTrait * @param Builder $model * @param array $where * @param bool $or - * @param string $field + * @param string $type * @throws Exception */ - private function addNewWhere(Builder $model, array $where, $or = false, $field = '') + private function addNewWhere(Builder $model, array $where, $or = false, string $type = 'and') { $method = $or ? 'orWhere' : 'where'; - $model->{$method}(function ($query) use ($where, $or, $field) { - $this->bindWhere($query, $where, $field === 'or'); + $model->{$method}(function ($query) use ($where, $or, $type) { + $this->bindWhere($query, $where, $type === 'or'); }); } @@ -287,10 +288,9 @@ trait RepositoryTrait * @param string $field * @param string|int|array $value * @param bool $or - * @return void * @throws \Exception */ - private function setFieldWhere(Builder $model, string $field, $value, $or = false): void + private function setFieldWhere(Builder $model, string $field, $value, $or = false) { [$field, $operator] = $this->formatField($field);