This commit is contained in:
2021-12-12 23:52:35 +08:00
parent 6e1d1a300a
commit 31a6862d62
+36 -7
View File
@@ -11,6 +11,7 @@ namespace Database;
use Database\Traits\QueryTrait;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Kiri\Abstracts\Component;
/**
@@ -63,8 +64,8 @@ class ActiveQuery extends Component implements ISqlBuilder
*/
public function clear()
{
$this->db = null;
$this->useCache = false;
$this->db = NULL;
$this->useCache = FALSE;
$this->with = [];
}
@@ -79,6 +80,34 @@ class ActiveQuery extends Component implements ISqlBuilder
return $this;
}
/**
* @param int $size
* @param int $page
* @return array
* @throws Exception
*/
#[ArrayShape(['size' => "int", 'page' => "int", 'count' => "int", 'next' => "int", 'prev' => "int", 'list' => "array"])]
public function pagination(int $size = 20, int $page = 1): array
{
$page = max(1, $page);
$size = max(1, $size);
$offset = ($page - 1) * $size;
$count = $this->count();
$lists = $this->limit($offset, $size)->get()->toArray();
return [
'size' => $size,
'page' => $page,
'count' => $count,
'next' => max($page + 1, 1),
'prev' => max($page - 1, 1),
'list' => $lists,
];
}
/**
* @param array $values
* @return $this
@@ -198,7 +227,7 @@ class ActiveQuery extends Component implements ISqlBuilder
public function all(): Collection|array
{
$data = $this->execute($this->builder->all())->all();
if (!empty($this->with)){
if (!empty($this->with)) {
$this->getWith($this->modelClass);
}
$collect = new Collection($this, $data, $this->modelClass);
@@ -266,7 +295,7 @@ class ActiveQuery extends Component implements ISqlBuilder
*/
public function batchInsert(array $data): bool
{
[$sql, $params] = $this->builder->insert($data, true);
[$sql, $params] = $this->builder->insert($data, TRUE);
return $this->execute($sql, $params)->exec();
@@ -280,7 +309,7 @@ class ActiveQuery extends Component implements ISqlBuilder
*/
public function value($filed)
{
return $this->first()[$filed] ?? null;
return $this->first()[$filed] ?? NULL;
}
/**
@@ -298,10 +327,10 @@ class ActiveQuery extends Component implements ISqlBuilder
* @return int|bool|string|null
* @throws Exception
*/
public function delete(bool $getSql = false): int|bool|string|null
public function delete(bool $getSql = FALSE): int|bool|string|null
{
$sql = $this->builder->delete();
if ($getSql === false) {
if ($getSql === FALSE) {
return $this->execute($sql)->delete();
}
return $sql;