From ee8b5fba20dbba9dbaf0e0b5af04ab79ea1b3bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Mon, 10 Apr 2023 17:20:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ActiveQuery.php | 26 ++++++++++++++++---------- Pagination.php | 2 +- Traits/QueryTrait.php | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/ActiveQuery.php b/ActiveQuery.php index 407f657..7f88ad9 100644 --- a/ActiveQuery.php +++ b/ActiveQuery.php @@ -92,7 +92,7 @@ class ActiveQuery extends Component implements ISqlBuilder $offset = ($page - 1) * $size; $count = $this->count(); - $lists = $this->limit($offset, $size)->get()->toArray(); + $lists = $this->offset($offset)->limit($size)->get()->toArray(); return [ 'code' => 0, 'message' => 'ok', @@ -153,12 +153,12 @@ class ActiveQuery extends Component implements ISqlBuilder /** - * @return ModelInterface|null - * @throws + * @return ModelInterface|array|null + * @throws Exception */ - public function first(): ModelInterface|null + public function first(): ModelInterface|null|array { - $data = $this->limit(0, 1)->execute($this->builder->one(), $this->attributes)->one(); + $data = $this->limit(1)->execute($this->builder->one(), $this->attributes)->one(); if (is_array($data)) { return $this->populate($data); } else { @@ -230,7 +230,8 @@ class ActiveQuery extends Component implements ISqlBuilder */ public function all(): Collection|array { - if (!($data = $this->execute($this->builder->all())->all())) { + $data = $this->execute($this->builder->all())->all(); + if ($data === false) { return new Collection($this, [], $this->modelClass); } @@ -241,12 +242,17 @@ class ActiveQuery extends Component implements ISqlBuilder /** * @param $data - * @return ModelInterface - * @throws + * @return ModelInterface|array + * @throws Exception */ - public function populate($data): ModelInterface + public function populate($data): ModelInterface|array { - return $this->modelClass::populate($data); + $model = $this->modelClass::populate($data); + if ($this->asArray) { + return $model->toArray(); + } else { + return $model; + } } diff --git a/Pagination.php b/Pagination.php index 4908e2b..85b7ba8 100644 --- a/Pagination.php +++ b/Pagination.php @@ -192,7 +192,7 @@ class Pagination extends Component if ($this->_max > 0 && $this->_length + $this->_limit > $this->_max) { $this->_limit = $this->_length + $this->_limit - $this->_max; } - $data = $this->activeQuery->limit($this->_offset, $this->_limit)->get(); + $data = $this->activeQuery->offset($this->_offset)->limit($this->_limit)->get(); $this->_offset += $this->_limit; if (is_array($data)) { diff --git a/Traits/QueryTrait.php b/Traits/QueryTrait.php index fd77524..e03cac6 100644 --- a/Traits/QueryTrait.php +++ b/Traits/QueryTrait.php @@ -808,15 +808,24 @@ trait QueryTrait } /** - * @param int $offset * @param int $limit * * @return $this */ - public function limit(int $offset, int $limit = 20): static + public function limit(int $limit = 20): static + { + $this->limit = $limit; + return $this; + } + + + /** + * @param int $offset + * @return $this + */ + public function offset(int $offset): static { $this->offset = $offset; - $this->limit = $limit; return $this; }