优化结构

main
gzydong 2021-08-29 23:14:38 +08:00
parent aafefbdffa
commit 43dbf226ec
2 changed files with 11 additions and 10 deletions

View File

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace App\Repository; namespace App\Repository;
use App\Traits\RepositoryTrait;
use Hyperf\Database\Model\Model; use Hyperf\Database\Model\Model;
use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
@ -183,7 +184,7 @@ abstract class BaseRepository
/** /**
* 通过 Model 读取分页数据 * 通过 Model 读取分页数据
* *
* @param Builder $model 查询 Model * @param Builder $model 查询构造器
* @param array $fields 查询字段 * @param array $fields 查询字段
* @param int $page 当前分页 * @param int $page 当前分页
* @param int $size 分页大小 * @param int $size 分页大小

View File

@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace App\Repository; namespace App\Traits;
use App\Helper\ArrayHelper; use App\Helper\ArrayHelper;
use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\Builder;
@ -109,6 +109,7 @@ trait RepositoryTrait
/** /**
* BaseRepository constructor. * BaseRepository constructor.
*
* @param Model $model * @param Model $model
*/ */
public function __construct(Model $model) public function __construct(Model $model)
@ -117,7 +118,7 @@ trait RepositoryTrait
} }
/** /**
* 获取 Model * 获取 Model 实例
* *
* @return Model * @return Model
*/ */
@ -214,7 +215,7 @@ trait RepositoryTrait
continue; continue;
} }
$this->addNewWhere($model, $item, $or, $field); $this->addNewWhere($model, $item, $or);
continue; continue;
} }
@ -229,15 +230,15 @@ trait RepositoryTrait
* @param Builder $model * @param Builder $model
* @param array $where * @param array $where
* @param bool $or * @param bool $or
* @param string $field * @param string $type
* @throws Exception * @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'; $method = $or ? 'orWhere' : 'where';
$model->{$method}(function ($query) use ($where, $or, $field) { $model->{$method}(function ($query) use ($where, $or, $type) {
$this->bindWhere($query, $where, $field === 'or'); $this->bindWhere($query, $where, $type === 'or');
}); });
} }
@ -287,10 +288,9 @@ trait RepositoryTrait
* @param string $field * @param string $field
* @param string|int|array $value * @param string|int|array $value
* @param bool $or * @param bool $or
* @return void
* @throws \Exception * @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); [$field, $operator] = $this->formatField($field);