This commit is contained in:
2023-04-10 17:20:48 +08:00
parent 94ebb18b7e
commit ee8b5fba20
3 changed files with 29 additions and 14 deletions
+16 -10
View File
@@ -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;
}
}
+1 -1
View File
@@ -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)) {
+12 -3
View File
@@ -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;
}