From 7430f86fb26784e7663d8918db1fde1f9493d35d Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Wed, 15 Sep 2021 21:52:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Repository/BaseRepository.php | 33 +++---------------------------- app/helper.php | 30 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/app/Repository/BaseRepository.php b/app/Repository/BaseRepository.php index 70d51ca..9c3254e 100644 --- a/app/Repository/BaseRepository.php +++ b/app/Repository/BaseRepository.php @@ -5,7 +5,7 @@ namespace App\Repository; use App\Traits\RepositoryTrait; use Hyperf\Database\Model\Model; -use Hyperf\Database\Model\Builder; +use Hyperf\Database\Model\Builder as ModelBuilder; use Hyperf\DbConnection\Db; use Hyperf\Utils\Collection; @@ -61,7 +61,7 @@ abstract class BaseRepository * @param array $where 查询条件 * @param string[] $fields 查询字段 * @param bool $is_array 是否返回数组格式 - * @return Builder|Model|object|array|null + * @return ModelBuilder|Model|object|array|null */ final public function first(array $where = [], array $fields = ['*'], bool $is_array = false) { @@ -112,7 +112,7 @@ abstract class BaseRepository $model = $this->buildWhere($where); - return $this->toPaginate($model, $fields, $page, $size); + return toPaginate($model, $fields, $page, $size); } /** @@ -192,31 +192,4 @@ abstract class BaseRepository { return Db::select($query, $bindings, $useReadPdo); } - - /** - * 通过 Model 读取分页数据 - * - * @param Builder $model 查询构造器 - * @param array $fields 查询字段 - * @param int $page 当前分页 - * @param int $size 分页大小 - * @return array - */ - public function toPaginate(Builder $model, array $fields = ['*'], int $page = 1, int $size = 15): array - { - $total = $model->count(); - - $data = [ - 'rows' => [], - 'paginate' => [ - 'page' => $page, - 'size' => $size, - 'total' => $total, - ] - ]; - - if ($total > 0) $data['rows'] = $model->forPage($page, $size)->get($fields)->toArray(); - - return $data; - } } diff --git a/app/helper.php b/app/helper.php index 1a0008a..a1b4005 100644 --- a/app/helper.php +++ b/app/helper.php @@ -6,6 +6,8 @@ */ use Hyperf\Contract\StdoutLoggerInterface; +use Hyperf\Database\Query\Builder as QueryBuilder; +use Hyperf\Database\Model\Builder as ModelBuilder; use Hyperf\HttpServer\Contract\ResponseInterface; use Hyperf\Logger\LoggerFactory; use Hyperf\Server\ServerFactory; @@ -224,3 +226,31 @@ function get_real_ip(): string return request()->getServerParams()['remote_addr'] ?? ''; } + + +/** + * 通过查询构造器读取分页数据 + * + * @param QueryBuilder|ModelBuilder $model 查询构造器 + * @param array $fields 查询字段 + * @param int $page 当前分页 + * @param int $size 分页大小 + * @return array + */ +function toPaginate($model, array $fields = ['*'], int $page = 1, int $size = 15): array +{ + $total = $model->count(); + + $data = [ + 'rows' => [], + 'paginate' => [ + 'page' => $page, + 'size' => $size, + 'total' => $total, + ] + ]; + + if ($total > 0) $data['rows'] = $model->forPage($page, $size)->get($fields)->toArray(); + + return $data; +}