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 Database\Traits\QueryTrait;
use Exception; use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
/** /**
@@ -63,8 +64,8 @@ class ActiveQuery extends Component implements ISqlBuilder
*/ */
public function clear() public function clear()
{ {
$this->db = null; $this->db = NULL;
$this->useCache = false; $this->useCache = FALSE;
$this->with = []; $this->with = [];
} }
@@ -79,6 +80,34 @@ class ActiveQuery extends Component implements ISqlBuilder
return $this; 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 * @param array $values
* @return $this * @return $this
@@ -198,7 +227,7 @@ class ActiveQuery extends Component implements ISqlBuilder
public function all(): Collection|array public function all(): Collection|array
{ {
$data = $this->execute($this->builder->all())->all(); $data = $this->execute($this->builder->all())->all();
if (!empty($this->with)){ if (!empty($this->with)) {
$this->getWith($this->modelClass); $this->getWith($this->modelClass);
} }
$collect = new Collection($this, $data, $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 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(); return $this->execute($sql, $params)->exec();
@@ -280,7 +309,7 @@ class ActiveQuery extends Component implements ISqlBuilder
*/ */
public function value($filed) 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 * @return int|bool|string|null
* @throws Exception * @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(); $sql = $this->builder->delete();
if ($getSql === false) { if ($getSql === FALSE) {
return $this->execute($sql)->delete(); return $this->execute($sql)->delete();
} }
return $sql; return $sql;