From 43dbf226ecae918d466a0acf1171571ef33602a3 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Sun, 29 Aug 2021 23:14:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Repository/BaseRepository.php | 3 ++- app/{Repository => Traits}/RepositoryTrait.php | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) rename app/{Repository => Traits}/RepositoryTrait.php (96%) 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);