This commit is contained in:
2023-07-25 09:33:24 +08:00
parent 4c6dbc9b3c
commit 4abc1a621b
3 changed files with 288 additions and 275 deletions
+279 -267
View File
@@ -21,314 +21,326 @@ use Kiri\Abstracts\Component;
class ActiveQuery extends Component implements ISqlBuilder class ActiveQuery extends Component implements ISqlBuilder
{ {
use QueryTrait; use QueryTrait;
/** @var bool */ /** @var bool */
public bool $asArray = FALSE; public bool $asArray = FALSE;
/** @var bool */ /** @var bool */
public bool $useCache = FALSE; public bool $useCache = FALSE;
/** /**
* @var Connection|null * @var Connection|null
*/ */
public ?Connection $db = NULL; public ?Connection $db = NULL;
/** /**
* @var array * @var array
* 参数绑定 * 参数绑定
*/ */
public array $attributes = []; public array $attributes = [];
protected mixed $_mock = null;
/** /**
* Comply constructor. * Comply constructor.
* @param $model * @param $model
* @param array $config * @throws
* @throws */
*/ public function __construct($model)
public function __construct($model, array $config = []) {
{ $this->modelClass = $model;
$this->modelClass = $model;
$this->builder = SqlBuilder::builder($this); $this->builder = SqlBuilder::builder($this);
parent::__construct(); parent::__construct();
} }
/** /**
* 清除不完整数据 * 清除不完整数据
*/ */
public function clear(): void public function clear(): void
{ {
$this->db = NULL; $this->db = NULL;
$this->useCache = FALSE; $this->useCache = FALSE;
} }
/** /**
* @param $key * @param $key
* @param $value * @param $value
* @return $this * @return $this
*/ */
public function addParam($key, $value): static public function addParam($key, $value): static
{ {
$this->attributes[$key] = $value; $this->attributes[$key] = $value;
return $this; return $this;
} }
/** /**
* @param int $size * @param int $size
* @param int $page * @param int $page
* @return array * @return array
* @throws * @throws
*/ */
#[ArrayShape([])] #[ArrayShape([])]
public function pagination(int $size = 20, int $page = 1): array public function pagination(int $size = 20, int $page = 1): array
{ {
$page = max(1, $page); $page = max(1, $page);
$size = max(1, $size); $size = max(1, $size);
$offset = ($page - 1) * $size; $offset = ($page - 1) * $size;
$count = $this->count(); $count = $this->count();
$lists = $this->offset($offset)->limit($size)->get()->toArray(); $lists = $this->offset($offset)->limit($size)->get()->toArray();
return [ return [
'code' => 0, 'code' => 0,
'message' => 'ok', 'message' => 'ok',
'size' => $size, 'size' => $size,
'page' => $page, 'page' => $page,
'count' => $count, 'count' => $count,
'next' => max($page + 1, 1), 'next' => max($page + 1, 1),
'prev' => max($page - 1, 1), 'prev' => max($page - 1, 1),
'param' => $lists, 'param' => $lists,
]; ];
} }
/** /**
* @param bool $asArray * @param bool $asArray
* @return static * @return static
*/ */
public function asArray(bool $asArray = true): static public function asArray(bool $asArray = true): static
{ {
$this->asArray = $asArray; $this->asArray = $asArray;
return $this; return $this;
} }
/** /**
* @param array $values * @param array $values
* @return $this * @return $this
*/ */
public function addParams(array $values): static public function addParams(array $values): static
{ {
foreach ($values as $key => $val) { foreach ($values as $key => $val) {
$this->addParam($key, $val); $this->addParam($key, $val);
} }
return $this; return $this;
} }
/** /**
* @param array $methods * @param array $methods
* @return $this * @return $this
*/ */
public function with(array $methods): static public function with(array $methods): static
{ {
$this->modelClass->setWith($methods); $this->modelClass->setWith($methods);
return $this; return $this;
} }
/** /**
* @param $sql * @param $sql
* @param array $params * @param array $params
* @return mixed * @return mixed
* @throws * @throws
*/ */
public function execute($sql, array $params = []): Command public function execute($sql, array $params = []): Command
{ {
return $this->modelClass->getConnection()->createCommand($sql, $params); return $this->modelClass->getConnection()->createCommand($sql, $params);
} }
/** /**
* @return ModelInterface|array|null * @return ModelInterface|array|null
* @throws Exception * @throws Exception
*/ */
public function first(): ModelInterface|null|array public function first(): ModelInterface|null|array
{ {
$data = $this->limit(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 {
return NULL; return NULL;
} }
} }
/** /**
* @return string * @return string
* @throws * @throws
*/ */
public function toSql(): string public function toSql(): string
{ {
return $this->builder->get(); return $this->builder->get();
} }
/** /**
* @return array|Collection * @return array|Collection
*/ * @throws Exception
public function get(): Collection|array */
{ public function get(): Collection|array
return $this->all(); {
} $data = $this->execute($this->builder->all(), $this->attributes)->all();
if ($data === false) {
return new Collection($this, [], $this->modelClass);
}
$collect = new Collection($this, $data, $this->modelClass);
return $this->asArray ? $collect->toArray() : $collect;
}
/** /**
* @throws * @throws
*/ */
public function flush(): array|bool|int|string|null public function flush(): array|bool|int|string|null
{ {
return $this->execute($this->builder->truncate())->exec(); return $this->execute($this->builder->truncate())->exec();
} }
/** /**
* @param int $size * @param int $size
* @param callable $callback * @param callable $callback
* @param int $offset * @param int $offset
* @return Pagination * @return Pagination
* @throws * @throws
*/ */
public function page(int $size, callable $callback, int $offset = 0): Pagination public function page(int $size, callable $callback, int $offset = 0): Pagination
{ {
$pagination = new Pagination($this); $pagination = new Pagination($this);
$pagination->setOffset($offset); $pagination->setOffset($offset);
$pagination->setLimit($size); $pagination->setLimit($size);
$pagination->setCallback($callback); $pagination->setCallback($callback);
return $pagination; return $pagination;
} }
/** /**
* @param string $field * @param string $field
* @param string $setKey * @param string $setKey
* *
* @return array|null * @return array|null
* @throws * @throws
*/ */
public function column(string $field, string $setKey = ''): ?array public function column(string $field, string $setKey = ''): ?array
{ {
return $this->all()->column($field, $setKey); return $this->get()->column($field, $setKey);
} }
/** /**
* @return array|Collection * @param mixed $value
* @throws * @return $this
*/ */
public function all(): Collection|array public function withMock(mixed $value): static
{ {
$data = $this->execute($this->builder->all(), $this->attributes)->all(); $this->_mock = $value;
if ($data === false) { return $this;
return new Collection($this, [], $this->modelClass); }
}
$collect = new Collection($this, $data, $this->modelClass);
return $this->asArray ? $collect->toArray() : $collect;
}
/**
* @param $data
* @return ModelInterface|array
* @throws Exception
*/
public function populate($data): ModelInterface|array
{
$model = $this->modelClass->populates($data);
if ($this->asArray) {
return $model->toArray();
} else {
return $model;
}
}
/** /**
* @return int * @return mixed
* @throws */
*/ public function mock(): mixed
public function count(): int {
{ return $this->_mock;
return $this->execute($this->builder->count(), $this->attributes)->one()['row_count'] ?? 0; }
}
/** /**
* @param array $data * @param $data
* @return bool * @return ModelInterface|array
* @throws * @throws Exception
*/ */
public function update(array $data): bool public function populate($data): ModelInterface|array
{ {
$generate = $this->builder->update($data); $model = $this->modelClass->populates($data);
if (is_bool($generate)) { if ($this->asArray) {
return $generate; return $model->toArray();
} } else {
return $model;
$generate[1] = array_merge($this->attributes, $generate[1]); }
}
return (bool)$this->execute(...$generate)->exec();
}
/**
* @param array $data
* @return bool
* @throws
*/
public function insert(array $data): bool
{
[$sql, $params] = $this->builder->insert($data, TRUE);
return (bool)$this->execute($sql, $params)->exec(); /**
} * @return int
* @throws
/** */
* @param $filed public function count(): int
* {
* @return null return $this->execute($this->builder->count(), $this->attributes)->one()['row_count'] ?? 0;
* @throws }
*/
public function value($filed)
{
return $this->first()[$filed] ?? NULL;
}
/**
* @return bool
* @throws
*/
public function exists(): bool
{
return !empty($this->execute($this->builder->one(), $this->attributes)->fetchColumn());
}
/** /**
* @param bool $getSql * @param array $data
* @return bool|string * @return bool
* @throws Exception * @throws
*/ */
public function delete(bool $getSql = FALSE): bool|string public function update(array $data): bool
{ {
$sql = $this->builder->delete(); $generate = $this->builder->update($data);
if ($getSql === FALSE) { if (is_bool($generate)) {
return (bool)$this->execute($sql, $this->attributes)->delete(); return $generate;
} }
return $sql;
} $generate[1] = array_merge($this->attributes, $generate[1]);
return (bool)$this->execute(...$generate)->exec();
}
/**
* @param array $data
* @return bool
* @throws
*/
public function insert(array $data): bool
{
[$sql, $params] = $this->builder->insert($data, TRUE);
return (bool)$this->execute($sql, $params)->exec();
}
/**
* @param $filed
*
* @return null
* @throws
*/
public function value($filed)
{
return $this->first()[$filed] ?? NULL;
}
/**
* @return bool
* @throws
*/
public function exists(): bool
{
return !empty($this->execute($this->builder->one(), $this->attributes)->fetchColumn());
}
/**
* @param bool $getSql
* @return bool|string
* @throws Exception
*/
public function delete(bool $getSql = FALSE): bool|string
{
$sql = $this->builder->delete();
if ($getSql === FALSE) {
return (bool)$this->execute($sql, $this->attributes)->delete();
}
return $sql;
}
} }
+2 -2
View File
@@ -229,7 +229,7 @@ class Model extends Base\Model
*/ */
public static function get($condition): Collection|array public static function get($condition): Collection|array
{ {
return static::query()->where($condition)->all(); return static::query()->where($condition)->get();
} }
@@ -246,7 +246,7 @@ class Model extends Base\Model
if (!empty($attributes)) { if (!empty($attributes)) {
$query->bindParams($attributes); $query->bindParams($attributes);
} }
return $query->all(); return $query->get();
} }
+7 -6
View File
@@ -60,7 +60,7 @@ class Relation extends Component
} }
$activeModel = $this->_query[$_identification]->first(); $activeModel = $this->_query[$_identification]->first();
if (empty($activeModel)) { if (empty($activeModel)) {
return null; return $this->_query[$_identification]->mock();
} }
unset($this->_query[$_identification]); unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel); return Context::set($_identification, $activeModel);
@@ -78,17 +78,18 @@ class Relation extends Component
} }
$activeModel = $this->_query[$_identification]->count(); $activeModel = $this->_query[$_identification]->count();
if (empty($activeModel)) { if (empty($activeModel)) {
return null; return $this->_query[$_identification]->mock();
} }
unset($this->_query[$_identification]); unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel); return Context::set($_identification, $activeModel);
} }
/** /**
* @param string $_identification * @param string $_identification
* @return mixed * @return mixed
*/ * @throws Exception
*/
public function get(string $_identification): mixed public function get(string $_identification): mixed
{ {
if (Context::exists($_identification)) { if (Context::exists($_identification)) {