变更
This commit is contained in:
+16
-10
@@ -92,7 +92,7 @@ class ActiveQuery extends Component implements ISqlBuilder
|
|||||||
$offset = ($page - 1) * $size;
|
$offset = ($page - 1) * $size;
|
||||||
|
|
||||||
$count = $this->count();
|
$count = $this->count();
|
||||||
$lists = $this->limit($offset, $size)->get()->toArray();
|
$lists = $this->offset($offset)->limit($size)->get()->toArray();
|
||||||
return [
|
return [
|
||||||
'code' => 0,
|
'code' => 0,
|
||||||
'message' => 'ok',
|
'message' => 'ok',
|
||||||
@@ -153,12 +153,12 @@ class ActiveQuery extends Component implements ISqlBuilder
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ModelInterface|null
|
* @return ModelInterface|array|null
|
||||||
* @throws
|
* @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)) {
|
if (is_array($data)) {
|
||||||
return $this->populate($data);
|
return $this->populate($data);
|
||||||
} else {
|
} else {
|
||||||
@@ -230,7 +230,8 @@ class ActiveQuery extends Component implements ISqlBuilder
|
|||||||
*/
|
*/
|
||||||
public function all(): Collection|array
|
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);
|
return new Collection($this, [], $this->modelClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,12 +242,17 @@ class ActiveQuery extends Component implements ISqlBuilder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $data
|
* @param $data
|
||||||
* @return ModelInterface
|
* @return ModelInterface|array
|
||||||
* @throws
|
* @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
@@ -192,7 +192,7 @@ class Pagination extends Component
|
|||||||
if ($this->_max > 0 && $this->_length + $this->_limit > $this->_max) {
|
if ($this->_max > 0 && $this->_length + $this->_limit > $this->_max) {
|
||||||
$this->_limit = $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;
|
$this->_offset += $this->_limit;
|
||||||
|
|
||||||
if (is_array($data)) {
|
if (is_array($data)) {
|
||||||
|
|||||||
+12
-3
@@ -808,15 +808,24 @@ trait QueryTrait
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $offset
|
|
||||||
* @param int $limit
|
* @param int $limit
|
||||||
*
|
*
|
||||||
* @return $this
|
* @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->offset = $offset;
|
||||||
$this->limit = $limit;
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user