This commit is contained in:
2023-04-10 17:13:24 +08:00
parent aaa3fba8ba
commit 94ebb18b7e
11 changed files with 409 additions and 767 deletions
+4 -23
View File
@@ -23,9 +23,6 @@ class ActiveQuery extends Component implements ISqlBuilder
use QueryTrait; use QueryTrait;
/** @var array */
public array $with = [];
/** @var bool */ /** @var bool */
public bool $asArray = FALSE; public bool $asArray = FALSE;
@@ -66,7 +63,6 @@ class ActiveQuery extends Component implements ISqlBuilder
{ {
$this->db = NULL; $this->db = NULL;
$this->useCache = FALSE; $this->useCache = FALSE;
$this->with = [];
} }
/** /**
@@ -134,14 +130,12 @@ class ActiveQuery extends Component implements ISqlBuilder
} }
/** /**
* @param array $name * @param array $methods
* @return $this * @return $this
*/ */
public function with(array $name): static public function with(array $methods): static
{ {
foreach ($name as $val) { $this->modelClass->setWith($methods);
$this->with[] = $val;
}
return $this; return $this;
} }
@@ -240,7 +234,6 @@ class ActiveQuery extends Component implements ISqlBuilder
return new Collection($this, [], $this->modelClass); return new Collection($this, [], $this->modelClass);
} }
$this->getWith($this->modelClass);
$collect = new Collection($this, $data, $this->modelClass); $collect = new Collection($this, $data, $this->modelClass);
return $this->asArray ? $collect->toArray() : $collect; return $this->asArray ? $collect->toArray() : $collect;
@@ -253,19 +246,10 @@ class ActiveQuery extends Component implements ISqlBuilder
*/ */
public function populate($data): ModelInterface public function populate($data): ModelInterface
{ {
return $this->getWith($this->modelClass::populate($data)); return $this->modelClass::populate($data);
} }
/**
* @param ModelInterface $model
* @return ModelInterface
*/
public function getWith(ModelInterface $model): ModelInterface
{
return $model->setWith($this->with);
}
/** /**
* @return int * @return int
* @throws * @throws
@@ -292,9 +276,6 @@ class ActiveQuery extends Component implements ISqlBuilder
return $generate; return $generate;
} }
var_dump($this->attributes, $generate[1]);
var_dump(array_merge($this->attributes, $generate[1]));
$generate[1] = array_merge($this->attributes, $generate[1]); $generate[1] = array_merge($this->attributes, $generate[1]);
return (bool)$this->execute(...$generate)->exec(); return (bool)$this->execute(...$generate)->exec();
+122 -105
View File
@@ -27,20 +27,28 @@ use Traversable;
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, ToArray abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, ToArray
{ {
/** /**
* @var ModelInterface[] * @var ModelInterface[]
*/ */
protected array $_item = []; protected array $_item = [];
protected ModelInterface|string|null $model;
protected ActiveQuery $query;
public function clean() /**
{ * @var ModelInterface|string|null
unset($this->query, $this->model, $this->_item); */
} protected ModelInterface|string|null $model;
/**
* @var ActiveQuery
*/
protected ActiveQuery $query;
public function clean()
{
unset($this->query, $this->model, $this->_item);
}
/** /**
@@ -51,112 +59,121 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
* @param ModelInterface|null $model * @param ModelInterface|null $model
* @throws Exception * @throws Exception
*/ */
public function __construct($query, array $array = [], ModelInterface $model = null) public function __construct($query, array $array = [], ModelInterface $model = null)
{ {
$this->_item = $array; $this->_item = $array;
$this->query = $query; $this->query = $query;
$this->model = $model; $this->model = $model;
parent::__construct([]); parent::__construct([]);
} }
/** /**
* @return int * @return int
*/ */
#[Pure] public function getLength(): int #[Pure] public function getLength(): int
{ {
return count($this->_item); return count($this->_item);
} }
/** /**
* @param $item * @param $item
*/ */
public function setItems($item) public function setItems($item)
{ {
$this->_item = $item; $this->_item = $item;
} }
/** /**
* @param $model * @param $model
*/ */
public function setModel($model) public function setModel($model)
{ {
$this->model = $model; $this->model = $model;
} }
/** /**
* @param $item * @param $item
*/ */
public function addItem($item) public function addItem($item)
{ {
$this->_item[] = $item; $this->_item[] = $item;
} }
/** /**
* @return Traversable|CollectionIterator|ArrayIterator * @return Traversable|CollectionIterator|ArrayIterator
* @throws Exception * @throws Exception
*/ */
public function getIterator(): Traversable|CollectionIterator|ArrayIterator public function getIterator(): Traversable|CollectionIterator|ArrayIterator
{ {
return new CollectionIterator($this->model, $this->_item); return new CollectionIterator($this->model, $this->_item);
} }
/** /**
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getModel(): ModelInterface public function getModel(): ModelInterface
{ {
return $this->model; return $this->model;
} }
/** /**
* @param mixed $offset * @return ActiveQuery
* @return bool */
*/ public function makeNewQuery(): ActiveQuery
public function offsetExists(mixed $offset): bool {
{ return $this->model::query();
return !empty($this->_item) && isset($this->_item[$offset]); }
}
/**
* @param mixed $offset
* @return ModelInterface|null
* @throws Exception
*/
public function offsetGet(mixed $offset): ?ModelInterface
{
if (!$this->offsetExists($offset)) {
return NULL;
}
if (!($this->_item[$offset] instanceof ModelInterface)) {
return $this->model->populates($this->_item[$offset]);
}
return $this->_item[$offset];
}
/**
* @param mixed $offset
* @param mixed $value
*/
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value)
{
$this->_item[$offset] = $value;
}
/** /**
* @param mixed $offset * @param mixed $offset
*/ * @return bool
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset) */
{ public function offsetExists(mixed $offset): bool
if ($this->offsetExists($offset)) { {
unset($this->_item[$offset]); return !empty($this->_item) && isset($this->_item[$offset]);
} }
}
/**
* @param mixed $offset
* @return ModelInterface|null
* @throws Exception
*/
public function offsetGet(mixed $offset): ?ModelInterface
{
if (!$this->offsetExists($offset)) {
return NULL;
}
if (!($this->_item[$offset] instanceof ModelInterface)) {
return $this->model->populates($this->_item[$offset]);
}
return $this->_item[$offset];
}
/**
* @param mixed $offset
* @param mixed $value
*/
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value)
{
$this->_item[$offset] = $value;
}
/**
* @param mixed $offset
*/
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset)
{
if ($this->offsetExists($offset)) {
unset($this->_item[$offset]);
}
}
} }
+126 -404
View File
@@ -46,28 +46,17 @@ use validator\Validator;
abstract class Model extends Component implements ModelInterface, ArrayAccess, ToArray abstract class Model extends Component implements ModelInterface, ArrayAccess, ToArray
{ {
const GET = 'get';
const SET = 'set';
/** @var array */ /** @var array */
protected array $_attributes = []; protected array $_attributes = [];
/** @var array */ /** @var array */
protected array $_oldAttributes = []; protected array $_oldAttributes = [];
/** @var array */
protected array $_relate = [];
/** @var null|string */ /** @var null|string */
protected ?string $primary = NULL; protected ?string $primary = NULL;
/**
* @var array
*/
private array $_annotations = [];
/** /**
* @var bool * @var bool
@@ -76,15 +65,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @var array * @var bool
*/ */
protected array $overwriteFields = []; protected bool $skipValidate = false;
/**
* @var array
*/
protected array $actions = [];
/** /**
@@ -105,6 +88,18 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
protected array $_with = []; protected array $_with = [];
/**
* @param array $config
* @throws Exception
*/
public function __construct(array $config = [])
{
parent::__construct($config);
$this->init();
}
/** /**
* @return array * @return array
*/ */
@@ -163,15 +158,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
/**
* @return array
*/
public function getActions(): array
{
return $this->actions;
}
/** /**
* @return bool * @return bool
*/ */
@@ -209,31 +195,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function hasPrimary(): bool public function hasPrimary(): bool
{ {
if ($this->primary !== NULL) { return $this->primary !== NULL && $this->primary !== '';
return TRUE;
}
$primary = $this->getColumns()->getPrimaryKeys();
if (!empty($primary)) {
return $this->primary = is_array($primary) ? current($primary) : $primary;
}
return FALSE;
}
/**
* @throws Exception
*/
public function isAutoIncrement(): bool
{
return $this->getAutoIncrement() !== NULL;
}
/**
* @throws Exception
*/
public function getAutoIncrement(): int|string|null
{
return $this->getColumns()->getAutoIncrement();
} }
/** /**
@@ -256,7 +218,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
public function hasPrimaryValue(): bool public function hasPrimaryValue(): bool
{ {
if ($this->hasPrimary()) { if ($this->hasPrimary()) {
return !empty($this->{$this->getPrimary()}); return $this->getPrimaryValue() === null;
} }
return false; return false;
} }
@@ -269,90 +231,23 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
public function getPrimaryValue(): ?int public function getPrimaryValue(): ?int
{ {
if ($this->hasPrimary()) { if ($this->hasPrimary()) {
return $this->getAttribute($this->primary); return $this->getAttribute($this->getPrimary());
} }
return null; return null;
} }
/** /**
* @param int|array|string|null $param * @param array|string $param
* @param null $db * @param null $db
* @return Model|null * @return Model|null
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public static function findOne(int|array|string|null $param, $db = NULL): static|null public static function findOne(array|string $param, $db = NULL): static|null
{ {
if (is_null($param)) {
return NULL;
}
if (is_numeric($param)) {
$param = static::getPrimaryCondition($param);
}
return static::query()->where($param)->first(); return static::query()->where($param)->first();
} }
/**
* @param $param
* @return array
* @throws Exception
*/
private static function getPrimaryCondition($param): array
{
$primary = static::makeNewInstance()->getColumns()->getPrimaryKeys();
if (empty($primary)) {
throw new Exception('Primary key cannot be empty.');
}
if (is_array($primary)) {
$primary = current($primary);
}
return [$primary => $param];
}
/**
* @param null $field
* @return ModelInterface|null
* @throws Exception
* @throws Exception
*/
public static function max($field = NULL): ?ModelInterface
{
$columns = static::makeNewInstance()->getColumns();
if (empty($field)) {
$field = $columns->getFirstPrimary();
}
$columns = $columns->get_fields();
if (!isset($columns[$field])) {
return NULL;
}
$first = static::query()->max($field)->first();
if (empty($first)) {
return NULL;
}
return $first[$field];
}
/**
* @param string|int $param
* @return Model|null
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function find(string|int $param): ?static
{
$columns = (new static())->getPrimary();
if (empty($columns)) {
$columns = static::makeNewInstance()->getColumns()->getFirstPrimary();
}
return static::query()->where([$columns => $param])->first();
}
/** /**
* @return static * @return static
*/ */
@@ -375,10 +270,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @return ActiveQuery * @return ActiveQuery
* @throws Exception
*/ */
public static function query(): ActiveQuery public static function query(): ActiveQuery
{ {
return new ActiveQuery(new static()); $model = new static();
return (new ActiveQuery($model))->from($model->getTable())->alias('t1');
} }
@@ -396,11 +293,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
* @param null $condition * @param null $condition
* @param array $attributes * @param array $attributes
* *
* @param bool $if_condition_is_null
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
protected static function deleteByCondition($condition = NULL, array $attributes = [], bool $if_condition_is_null = FALSE): bool protected static function deleteByCondition($condition = NULL, array $attributes = []): bool
{ {
$model = static::query(); $model = static::query();
if (!empty($condition)) { if (!empty($condition)) {
@@ -435,9 +331,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function setAttribute($name, $value): mixed public function setAttribute($name, $value): mixed
{ {
$keys = Kiri::getDi()->get(Setter::class); $method = 'set' . ucfirst($name) . 'Attribute';
if ($keys->has(static::class, $name)) { if (method_exists($this, $method)) {
$method = $keys->get(static::class, $name);
$value = $this->{$method}($value); $value = $this->{$method}($value);
} }
return $this->_attributes[$name] = $value; return $this->_attributes[$name] = $value;
@@ -451,8 +346,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function setOldAttribute($name, $value): mixed public function setOldAttribute($name, $value): mixed
{ {
if (method_exists($this, 'set' . ucfirst($name) . 'Attribute')) { $method = 'set' . ucfirst($name) . 'Attribute';
$value = $this->{'set' . ucfirst($name) . 'Attribute'}($value); if (method_exists($this, $method)) {
$value = $this->{$method}($value);
} }
return $this->_oldAttributes[$name] = $value; return $this->_oldAttributes[$name] = $value;
} }
@@ -464,7 +360,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function setAttributes(array $param): static public function setAttributes(array $param): static
{ {
if (empty($param)) { if (count($param) < 1) {
return $this; return $this;
} }
foreach ($param as $key => $attribute) { foreach ($param as $key => $attribute) {
@@ -475,13 +371,13 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @param $param * @param array $param
* @return $this * @return $this
* @throws ReflectionException * @throws ReflectionException
*/ */
public function setOldAttributes($param): static public function setOldAttributes(array $param): static
{ {
if (empty($param) || !is_array($param)) { if (count($param) < 1) {
return $this; return $this;
} }
foreach ($param as $key => $attribute) { foreach ($param as $key => $attribute) {
@@ -492,105 +388,96 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @param $attributes
* @param $param
* @return $this|bool * @return $this|bool
* @throws Exception * @throws Exception
*/ */
private function insert($param, $attributes): bool|static private function insert(): bool|static
{ {
[$sql, $param] = SqlBuilder::builder(static::query())->insert($param); [$sql, $param] = SqlBuilder::builder(static::query())->insert($this->_attributes);
$dbConnection = $this->getConnection()->createCommand($sql, $param); $dbConnection = $this->getConnection()->createCommand($sql, $param);
$lastId = $dbConnection->save(); $lastId = $dbConnection->save();
if ($this->isAutoIncrement()) { if ($lastId === false) {
$lastId = $this->setPrimary((int)$lastId, $param); return false;
} else {
$lastId = $this;
} }
if ($this->hasPrimary()) {
$this->setIsNowExample(false); $this->_attributes[$this->getPrimary()] = $lastId;
$this->refresh()->afterSave($attributes, $param);
return $lastId;
}
/**
* @param $lastId
* @param $param
* @return static
* @throws Exception
*/
private function setPrimary($lastId, $param): static
{
if ($this->isAutoIncrement()) {
$this->setAttribute($this->getAutoIncrement(), (int)$lastId);
return $this;
}
if (!$this->hasPrimary()) {
return $this;
}
$primary = $this->getPrimary();
if (empty($param[$primary])) {
$this->setAttribute($primary, (int)$lastId);
} }
return $this; return $this;
} }
/** /**
* @param $fields * @param array $old
* @param $condition * @param array|string $condition
* @param $param * @param array $change
* @return $this|bool * @return $this|bool
* @throws Exception * @throws Exception
*/ */
private function updateInternal($fields, $condition, $param): bool|static protected function updateInternal(array $old, array|string $condition, array $change): bool|static
{ {
if (empty($param)) {
return TRUE;
}
if ($this->hasPrimary()) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
}
$query = static::query()->where($condition); $query = static::query()->where($condition);
$generate = SqlBuilder::builder($query)->update($param); $generate = SqlBuilder::builder($query)->update($change);
if (is_bool($generate)) { if ($generate === false) {
return $generate; return false;
} }
$generate[1] = array_merge($query->attributes, $generate[1]); $command = $this->getConnection()->createCommand($generate, $query->attributes);
$command = $this->getConnection()->createCommand($generate[0], $generate[1]);
if ($command->save()) { if ($command->save()) {
return $this->refresh()->afterSave($fields, $param); return $this->refresh()->afterSave($old, $change);
} else {
return FALSE;
} }
return FALSE;
} }
/** /**
* @param null $data * @param array $data
* @return bool|$this * @return bool|$this
* @throws Exception * @throws Exception
*/ */
public function save($data = NULL): static|bool public function save(array $data = []): static|bool
{ {
if (!is_null($data)) { if (count($data) > 0) {
$this->_attributes = merge($this->_attributes, $data); $this->_attributes = array_merge($this->_attributes, $data);
} }
if (!$this->isNewExample) {
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE;
}
[$changes, $condition] = $this->diff();
return $this->updateInternal($condition, $condition, $changes);
} else {
return $this->create();
}
}
/**
* @return array<array, array>
*/
private function diff(): array
{
$changes = array_diff_assoc($this->_oldAttributes, $this->_attributes);
$condition = array_intersect_key($this->_oldAttributes, $this->_attributes);
return [$changes, $condition];
}
/**
* @return $this|bool
* @throws Exception
*/
protected function create(): bool|static
{
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) { if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE; return FALSE;
} }
[$change, $condition, $fields] = $this->separation(); return $this->insert();
if (!$this->getIsNowExample()) {
return $this->updateInternal($fields, $condition, $change);
} else {
return $this->insert($change, $fields);
}
} }
@@ -602,19 +489,21 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
$this->_attributes = $value; $this->_attributes = $value;
$this->_oldAttributes = $value; $this->_oldAttributes = $value;
$this->setIsNowExample(FALSE); $this->setIsNowExample();
return $this; return $this;
} }
/** /**
* @param array|null $rule * @param array $rule
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function validator(?array $rule): bool public function validator(array $rule): bool
{ {
if (empty($rule)) return TRUE; if (count($rule) < 1 || $this->skipValidate) {
return TRUE;
}
$validate = $this->resolve($rule); $validate = $this->resolve($rule);
if (!$validate->validation()) { if (!$validate->validation()) {
return $this->logger->addError($validate->getError(), 'mysql'); return $this->logger->addError($validate->getError(), 'mysql');
@@ -630,9 +519,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
private function resolve($rule): Validator private function resolve($rule): Validator
{ {
$validate = Validator::getInstance(); $validate = Validator::instance($this->_attributes, $this);
$validate->setParams($this->_attributes);
$validate->setModel($this);
foreach ($rule as $val) { foreach ($rule as $val) {
$field = array_shift($val); $field = array_shift($val);
@@ -652,97 +539,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
/**
* @param string $name
* @param mixed $value
* @param string $type
* @return mixed
*/
protected function runAnnotation(string $name, mixed $value, string $type = self::GET): mixed
{
return call_user_func($this->_annotations[$type][$name], $value);
}
/**
* @return array
* @throws Exception
*/
private function separation(): array
{
$assoc = array_diff_assoc($this->_attributes, $this->_oldAttributes);
$column = $this->getColumns();
$uassoc = array_intersect_assoc($this->_attributes, $this->_oldAttributes);
foreach ($assoc as $key => $item) {
$encode = $column->get_fields($key);
if ($column->isString($encode) && $item === null) {
unset($assoc[$key]);
}
}
return [$assoc, $uassoc, array_keys($assoc)];
}
/**
* @param $columns
* @param $format
* @param $key
* @param $value
* @return mixed
*/
public function toFormat($columns, $format, $key, $value): mixed
{
if (isset($format[$key])) {
return $columns->encode($value, $columns->clean($format[$key]));
}
return $value;
}
/**
* @param $name
* @param $value
*/
public function setRelate($name, $value)
{
$this->_relate[$name] = $value;
}
/**
* @param $name
* @return bool
*/
public function hasRelate($name): bool
{
return isset($this->_relate[$name]);
}
/**
* @param array $relates
*/
public function setRelates(array $relates)
{
if (empty($relates)) {
return;
}
foreach ($relates as $key => $val) {
$this->setRelate($key, $val);
}
}
/**
* @return array
*/
public function getRelates(): array
{
return $this->_relate;
}
/** /**
* @return Relation|null * @return Relation|null
*/ */
@@ -759,7 +555,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function has($attribute): bool public function has($attribute): bool
{ {
return static::makeNewInstance()->getColumns()->hasField($attribute); return true;
} }
/**ƒ /**ƒ
@@ -783,22 +579,22 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @param $attributes * @param $oldAttributes
* @param $changeAttributes * @param $changeAttributes
* @return bool * @return bool
* @throws Exception
*/ */
public function afterSave($attributes, $changeAttributes): bool public function afterSave($oldAttributes, $changeAttributes): bool
{ {
return TRUE; return TRUE;
} }
/** /**
* @param $model * @param self $model
* @return bool * @return bool
* @throws Exception
*/ */
public function beforeSave($model): bool public function beforeSave(self $model): bool
{ {
return TRUE; return TRUE;
} }
@@ -820,13 +616,14 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function __set($name, $value): void public function __set($name, $value): void
{ {
$method = 'set' . ucfirst($name); if ($this->hasRelateMethod($name, 'set')) {
if (method_exists($this, $method)) { $this->{'set' . ucfirst($name)}($value);
$this->{$method}($value);
} else { } else {
$overrideSetter = Kiri::getDi()->get(Setter::class); $method = 'set' . ucfirst($name) . 'Attribute';
if (method_exists($this, $method)) {
$this->_attributes[$name] = $overrideSetter->override($this, $name, $value); $value = $this->{$method} ($value);
}
$this->_attributes[$name] = $value;
} }
} }
@@ -838,10 +635,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function __get($name): mixed public function __get($name): mixed
{ {
if (isset($this->_attributes[$name])) { $value = $this->_attributes[$name] ?? null;
return $this->withPropertyOverride($name); if (!$this->hasRelateMethod($name)) {
return $this->withPropertyOverride($name, $value);
} else { } else {
return $this->getRelateValue($name); return $this->withRelate($name);
} }
} }
@@ -854,23 +652,23 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
protected function withPropertyOverride($name, $value = null): mixed protected function withPropertyOverride($name, $value = null): mixed
{ {
if (is_null($value)) { $method = 'get' . ucfirst($name) . 'Attribute';
$value = $this->_attributes[$name] ?? NULL; if (method_exists($this, $method)) {
return $this->{$method}($value);
} else {
return $value;
} }
$overrideGetter = Kiri::getDi()->get(Getter::class);
return $overrideGetter->override($this, $name, $value);
} }
/** /**
* @param $name * @param string $name
* @param string $prefix
* @return bool * @return bool
*/ */
protected function hasRelateMethod($name): bool protected function hasRelateMethod(string $name, string $prefix = 'get'): bool
{ {
return method_exists($this, 'get' . ucfirst($name)); return method_exists($this, $prefix . ucfirst($name));
} }
@@ -878,25 +676,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
* @param $name * @param $name
* @return mixed|null * @return mixed|null
*/ */
protected function withRelate($name): mixed private function withRelate($name): mixed
{ {
$response = $this->getRelateValue($name);
if ($response instanceof ToArray) {
$response = $response->toArray();
}
return $response;
}
/**
* @param $name
* @return mixed
*/
protected function getRelateValue($name): mixed
{
if (!$this->hasRelateMethod($name)) {
return null;
}
$response = $this->{'get' . ucfirst($name)}(); $response = $this->{'get' . ucfirst($name)}();
if ($response instanceof HasBase) { if ($response instanceof HasBase) {
$response = $response->get(); $response = $response->get();
@@ -905,42 +686,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
/**
* @param $name
* @param $value
* @return mixed
* @throws Exception
*/
protected function _decode($name, $value): mixed
{
return $this->getColumns()->_decode($name, $value);
}
/**
* @param $name
* @return mixed
*/
private function with($name): mixed
{
$data = $this->{$this->_relate[$name]}();
if ($data instanceof HasBase) {
return $data->get();
}
return $data;
}
/**
* @param $item
* @param $data
* @return array
*/
protected function resolveAttributes($item, $data): array
{
return call_user_func($item, $data);
}
/** /**
* @param $name * @param $name
* @return bool * @return bool
@@ -993,9 +738,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
unset($this->_attributes[$offset]); unset($this->_attributes[$offset]);
unset($this->_oldAttributes[$offset]); unset($this->_oldAttributes[$offset]);
if (isset($this->_relate)) {
unset($this->_relate[$offset]);
}
} }
/** /**
@@ -1018,6 +760,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
->table($this->getTable()); ->table($this->getTable());
} }
/** /**
* @param array $data * @param array $data
* @return static * @return static
@@ -1025,33 +768,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public static function populate(array $data): static public static function populate(array $data): static
{ {
$model = instance(static::class); $model = new static();
$model->_attributes = $data; $model->_attributes = $data;
$model->_oldAttributes = $data; $model->_oldAttributes = $data;
$model->setIsNowExample(FALSE); $model->setIsNowExample();
return $model; return $model;
} }
/**
* @return void
*/
public function __clone(): void
{
}
/**
* @param $method
* @param $value
* @return Closure
*/
protected function dispatcher($method, $value): Closure
{
return function () use ($method, $value) {
return $this->{$method}($value);
};
}
/** /**
+26 -26
View File
@@ -28,19 +28,19 @@ class Command extends Component
const FETCH_ALL = 'fetchAll'; const FETCH_ALL = 'fetchAll';
const EXECUTE = 'execute'; const EXECUTE = 'execute';
const FETCH_COLUMN = 'fetchColumn'; const FETCH_COLUMN = 'fetchColumn';
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.'; const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
/** @var Connection */ /** @var Connection */
public Connection $connection; public Connection $connection;
/** @var ?string */ /** @var ?string */
public ?string $sql = ''; public ?string $sql = '';
/** @var array */ /** @var array */
public array $params = []; public array $params = [];
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws Exception
@@ -49,7 +49,7 @@ class Command extends Component
{ {
return $this->_execute(); return $this->_execute();
} }
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws Exception
@@ -58,13 +58,13 @@ class Command extends Component
{ {
return $this->_execute(); return $this->_execute();
} }
/** /**
* @return bool|array|null * @return bool|array
* @throws Exception * @throws Exception
*/ */
public function all(): null|bool|array public function all(): bool|array
{ {
try { try {
$client = $this->connection->getConnection(); $client = $this->connection->getConnection();
@@ -82,7 +82,7 @@ class Command extends Component
$this->connection->release($client ?? null); $this->connection->release($client ?? null);
} }
} }
/** /**
* @return bool|array|null * @return bool|array|null
* @throws Exception * @throws Exception
@@ -105,7 +105,7 @@ class Command extends Component
$this->connection->release($client ?? null); $this->connection->release($client ?? null);
} }
} }
/** /**
* @return bool|array|null * @return bool|array|null
* @throws Exception * @throws Exception
@@ -128,7 +128,7 @@ class Command extends Component
$this->connection->release($client ?? null); $this->connection->release($client ?? null);
} }
} }
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws Exception
@@ -151,8 +151,8 @@ class Command extends Component
$this->connection->release($client ?? null); $this->connection->release($client ?? null);
} }
} }
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws Exception
@@ -161,7 +161,7 @@ class Command extends Component
{ {
return $this->_execute(); return $this->_execute();
} }
/** /**
* @return bool|int * @return bool|int
* @throws ConfigException * @throws ConfigException
@@ -179,7 +179,7 @@ class Command extends Component
} }
$result = $client->lastInsertId(); $result = $client->lastInsertId();
$prepare->closeCursor(); $prepare->closeCursor();
if (!$client->inTransaction()) { if (!$client->inTransaction()) {
$this->connection->release($client); $this->connection->release($client);
} }
@@ -191,8 +191,8 @@ class Command extends Component
return $this->error($throwable); return $this->error($throwable);
} }
} }
/** /**
* @param \Throwable $throwable * @param \Throwable $throwable
* @return bool * @return bool
@@ -202,8 +202,8 @@ class Command extends Component
$message = $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE); $message = $this->sql . '.' . json_encode($this->params, JSON_UNESCAPED_UNICODE);
return $this->logger->addError($message . $throwable->getMessage(), 'mysql'); return $this->logger->addError($message . $throwable->getMessage(), 'mysql');
} }
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws Exception
@@ -212,7 +212,7 @@ class Command extends Component
{ {
return $this->_execute(); return $this->_execute();
} }
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws Exception
@@ -221,7 +221,7 @@ class Command extends Component
{ {
return $this->_execute(); return $this->_execute();
} }
/** /**
* @param array $data * @param array $data
* @return $this * @return $this
@@ -233,7 +233,7 @@ class Command extends Component
} }
return $this; return $this;
} }
/** /**
* @param $sql * @param $sql
* @return $this * @return $this
@@ -244,5 +244,5 @@ class Command extends Component
$this->sql = $sql; $this->sql = $sql;
return $this; return $this;
} }
} }
+37 -19
View File
@@ -9,6 +9,7 @@ declare(strict_types=1);
namespace Database; namespace Database;
use Closure;
use Database\Affair\Commit; use Database\Affair\Commit;
use Database\Affair\Rollback; use Database\Affair\Rollback;
use Database\Traits\QueryTrait; use Database\Traits\QueryTrait;
@@ -50,10 +51,35 @@ class Db implements ISqlBuilder
} }
/**
* @param Closure $closure
* @param mixed ...$params
* @return mixed
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public static function Transaction(Closure $closure, ...$params): mixed
{
static::beginTransaction();
try {
$result = call_user_func($closure, ...$params);
} catch (\Throwable $throwable) {
$result = logger()->addError($throwable->getMessage(), 'mysql');
} finally {
if ($result === false) {
static::rollback();
} else {
static::commit();
}
return $result;
}
}
/** /**
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
public static function commit(): void public static function commit(): void
{ {
@@ -67,7 +93,6 @@ class Db implements ISqlBuilder
* @return void * @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
public static function rollback(): void public static function rollback(): void
{ {
@@ -290,11 +315,11 @@ class Db implements ISqlBuilder
/** /**
* @param string $table * @param string $table
* @param Connection|NULL $connection * @param Connection|NULL $connection
* @return mixed * @return array|bool|null
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public static function showCreateSql(string $table, Connection $connection = NULL): mixed public static function showCreateSql(string $table, Connection $connection = NULL): array|bool|null
{ {
$connection = static::getDefaultConnection($connection); $connection = static::getDefaultConnection($connection);
@@ -305,11 +330,11 @@ class Db implements ISqlBuilder
/** /**
* @param string $table * @param string $table
* @param Connection|NULL $connection * @param Connection|NULL $connection
* @return mixed * @return array|bool|null
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public static function desc(string $table, Connection $connection = NULL): mixed public static function desc(string $table, Connection $connection = NULL): array|bool
{ {
$connection = static::getDefaultConnection($connection); $connection = static::getDefaultConnection($connection);
@@ -321,12 +346,12 @@ class Db implements ISqlBuilder
/** /**
* @param string $table * @param string $table
* @param Connection|NULL $connection * @param Connection|NULL $connection
* @return mixed * @return array|bool|null
* @throws Exception * @throws Exception
*/ */
public static function show(string $table, Connection $connection = NULL): mixed public static function show(string $table, Connection $connection = NULL): array|bool|null
{ {
if (empty($table)) { if ($table == '') {
return null; return null;
} }
$connection = static::getDefaultConnection($connection); $connection = static::getDefaultConnection($connection);
@@ -342,12 +367,11 @@ class Db implements ISqlBuilder
/** /**
* @param null|Connection $connection * @param null|Connection $connection
* @param null $name * @param string $name
* @return mixed * @return mixed
* @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public static function getDefaultConnection(?Connection $connection, $name = null): Connection public static function getDefaultConnection(?Connection $connection, string $name = 'db'): Connection
{ {
if ($connection instanceof Connection) { if ($connection instanceof Connection) {
return $connection; return $connection;
@@ -356,13 +380,7 @@ class Db implements ISqlBuilder
if (empty($databases) || !is_array($databases)) { if (empty($databases) || !is_array($databases)) {
throw new Exception('Please configure the database link.'); throw new Exception('Please configure the database link.');
} }
if (!empty($name)) { return \Kiri::service()->get($databases[$name]);
if (!isset($databases[$name])) {
throw new Exception('Please configure the database link.');
}
return $databases[$name];
}
return current($databases);
} }
+54 -66
View File
@@ -15,6 +15,8 @@ use Exception;
use Kiri; use Kiri;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
use Kiri\Error\StdoutLoggerInterface; use Kiri\Error\StdoutLoggerInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException; use ReflectionException;
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227); defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
@@ -99,31 +101,20 @@ class Model extends Base\Model
/** /**
* @param array $condition * @param array $condition
* @param array $attributes * @param array $attributes
* @return bool|ModelInterface * @return bool|static
* @throws ReflectionException * @throws ContainerExceptionInterface
* @throws NotFindClassException * @throws NotFoundExceptionInterface
* @throws Exception
*/ */
public static function findOrCreate(array $condition, array $attributes): bool|static public static function findOrCreate(array $condition, array $attributes): bool|static
{ {
$logger = Kiri::getDi()->get(StdoutLoggerInterface::class); return Db::Transaction(function ($condition, $attributes) {
if (empty($attributes)) { /** @var static $select */
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); $select = static::query()->where($condition)->first();
} if ($select === null) {
$select = static::populate(array_merge($condition, $attributes))->create();
/** @var static $select */ }
$select = static::query()->where($condition)->first();
if (!empty($select)) {
return $select; return $select;
} }, $condition, $attributes);
$select = new static();
$select->setAttributes($condition);
$select->setAttributes($attributes);
if (!$select->save()) {
throw new Exception($select->getLastError());
}
return $select;
} }
@@ -131,25 +122,19 @@ class Model extends Base\Model
* @param array $condition * @param array $condition
* @param array $attributes * @param array $attributes
* @return bool|static * @return bool|static
* @throws Exception * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public static function createOrUpdate(array $condition, array $attributes = []): bool|static public static function createOrUpdate(array $condition, array $attributes = []): bool|static
{ {
$logger = Kiri::getDi()->get(StdoutLoggerInterface::class); return Db::Transaction(function ($condition, $attributes) {
if (empty($attributes)) { /** @var static $select */
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); $select = static::query()->where($condition)->first();
} if (empty($select)) {
/** @var static $select */ $select = static::populate($condition);
$select = static::query()->where($condition)->first(); }
if (empty($select)) { return $select->save($attributes);
$select = new static(); }, $condition, $attributes);
$select->setAttributes($condition);
}
$select->setAttributes($attributes);
if (!$select->save()) {
throw new Exception($select->getLastError());
}
return $select;
} }
@@ -168,18 +153,26 @@ class Model extends Base\Model
if (is_bool($create)) { if (is_bool($create)) {
return false; return false;
} }
return $this->getConnection()->createCommand($create[0], $create[1])->exec(); return $this->getConnection()->createCommand($create, $activeQuery->attributes)->exec();
} }
/** /**
* @param array $fields * @param array $params
* @return ModelInterface|bool * @return ModelInterface|bool
* @throws Exception * @throws Exception
*/ */
public function update(array $fields): static|bool public function update(array $params): static|bool
{ {
return $this->save($fields); if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE;
}
$condition = array_diff_assoc($this->_oldAttributes, $params);
$old = array_intersect_key($this->_oldAttributes, $params);
return $this->updateInternal($old, $condition, $params);
} }
@@ -202,18 +195,13 @@ class Model extends Base\Model
*/ */
public function delete(): bool public function delete(): bool
{ {
$primary = $this->getPrimary(); if ($this->beforeDelete()) {
if (empty($primary) || !$this->hasPrimaryValue()) { $result = static::deleteByCondition($this->_attributes);
return $this->logger->addError("Only primary key operations are supported.", 'mysql');
} return $this->afterDelete($result);
if (!$this->beforeDelete()) { } else {
return false; return false;
} }
$result = static::deleteByCondition([$primary => $this->getPrimaryValue()]);
$this->afterDelete($result);
return $result;
} }
@@ -265,24 +253,23 @@ class Model extends Base\Model
public function toArray(): array public function toArray(): array
{ {
$data = $this->_attributes; $data = $this->_attributes;
foreach ($this->overwriteFields as $key => $datum) { foreach ($data as $key => $datum) {
$method = 'get' . ucfirst($key) . 'Attribute'; $method = 'get' . ucfirst($key) . 'Attribute';
if (!method_exists($this, $method)) {
continue;
}
$data[$key] = $this->{$method}($datum); $data[$key] = $this->{$method}($datum);
} }
return $this->withRelates($data);
}
/** $with = $this->getWith();
* @param $relates foreach ($with as $value) {
* @return array $join = $this->{'get' . ucfirst($value)}();
* @throws Exception if ($join instanceof Kiri\ToArray) {
*/ $join = $join->toArray();
private function withRelates($relates): array }
{ $data[$value] = $join;
foreach ($this->_with as $val) {
$relates[$val] = $this->withRelate($val);
} }
return $relates; return $data;
} }
@@ -372,10 +359,11 @@ class Model extends Base\Model
/** /**
* @param bool $result * @param bool $result
* @return void * @return bool
*/ */
public function afterDelete(bool $result): void public function afterDelete(bool $result): bool
{ {
return $result;
} }
/** /**
+2 -9
View File
@@ -18,18 +18,11 @@ interface ModelInterface
{ {
/** /**
* @param int|array|string|null $param * @param array|string $param
* @param null $db * @param null $db
* @return ModelInterface * @return ModelInterface
*/ */
public static function findOne(int|array|string|null $param, $db = NULL): mixed; public static function findOne(array|string $param, $db = NULL): mixed;
/**
* @param string|int $param
* @return ModelInterface
*/
public static function find(string|int $param): mixed;
/** /**
+1 -1
View File
@@ -128,7 +128,7 @@ class Pagination extends Component
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function plunk(array $param = []) public function plunk(array $param = []): void
{ {
$this->loop($param); $this->loop($param);
} }
+20 -47
View File
@@ -60,11 +60,9 @@ class SqlBuilder extends Component
* @return bool|array * @return bool|array
* @throws Exception * @throws Exception
*/ */
public function update(array $attributes): bool|array public function update(array $attributes): bool|string
{ {
[$string, $array] = $this->builderParams($attributes); return $this->__updateBuilder($this->builderParams($attributes));
return $this->__updateBuilder($string, $array);
} }
@@ -74,31 +72,28 @@ class SqlBuilder extends Component
* @return bool|array * @return bool|array
* @throws Exception * @throws Exception
*/ */
public function mathematics(array $attributes, string $opera = '+'): bool|array public function mathematics(array $attributes, string $opera = '+'): bool|string
{ {
$string = []; $string = [];
foreach ($attributes as $attribute => $value) { foreach ($attributes as $attribute => $value) {
$string[] = $attribute . '=' . $attribute . $opera . $value; $string[] = $attribute . '=' . $attribute . $opera . $value;
} }
return $this->__updateBuilder($string, []); return $this->__updateBuilder($string);
} }
/** /**
* @param array $string * @param array $string
* @param array $params * @return string|bool
* @return array|bool
* @throws Exception * @throws Exception
*/ */
private function __updateBuilder(array $string, array $params): array|bool private function __updateBuilder(array $string): string|bool
{ {
if (empty($string)) { if (empty($string)) {
return $this->logger->addError('None data update.'); return $this->logger->addError('None data update.');
} }
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $this->_prefix(); return 'UPDATE ' . $this->query->from . ' SET ' . implode(',', $string) . $this->_prefix();
return [$update, $params];
} }
@@ -110,7 +105,7 @@ class SqlBuilder extends Component
*/ */
public function insert(array $attributes, bool $isBatch = false): array public function insert(array $attributes, bool $isBatch = false): array
{ {
$update = 'INSERT INTO ' . $this->tableName(); $update = 'INSERT INTO ' . $this->query->from;
if ($isBatch === false) { if ($isBatch === false) {
$attributes = [$attributes]; $attributes = [$attributes];
} }
@@ -119,7 +114,7 @@ class SqlBuilder extends Component
$order = 0; $order = 0;
$keys = $params = []; $keys = $params = [];
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
[$_keys, $params] = $this->builderParams($attribute, true, $params, $order); $_keys = $this->builderParams($attribute, true, $order);
$keys[] = implode(',', $_keys); $keys[] = implode(',', $_keys);
$order++; $order++;
@@ -134,7 +129,7 @@ class SqlBuilder extends Component
*/ */
public function delete(): string public function delete(): string
{ {
return 'DELETE FROM ' . $this->tableName() . ' WHERE ' . $this->_prefix(); return 'DELETE FROM ' . $this->query->from . ' WHERE ' . $this->_prefix();
} }
@@ -151,23 +146,22 @@ class SqlBuilder extends Component
/** /**
* @param array $attributes * @param array $attributes
* @param bool $isInsert * @param bool $isInsert
* @param array $params
* @param int $order * @param int $order
* @return array[] * @return array[]
* a=:b, * a=:b,
*/ */
#[Pure] private function builderParams(array $attributes, bool $isInsert = false, array $params = [], int $order = 0): array private function builderParams(array $attributes, bool $isInsert = false, int $order = 0): array
{ {
$keys = []; $keys = [];
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {
if ($isInsert === true) { if ($isInsert === true) {
$keys[] = ':save' . $key . $order; $keys[] = ':save' . $key . $order;
$params[':save' . $key . $order] = $value; $this->query->bindParam(':save' . $key . $order, $value);
} else { } else {
[$keys, $params] = $this->resolveParams($key, $value, $order, $params, $keys); $keys = $this->resolveParams($key, $value, $order, $keys);
} }
} }
return [$keys, $params]; return $keys;
} }
@@ -175,14 +169,13 @@ class SqlBuilder extends Component
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param int $order * @param int $order
* @param array $params
* @param array $keys * @param array $keys
* @return array * @return array
*/ */
private function resolveParams(string $key, mixed $value, int $order, array $params, array $keys): array private function resolveParams(string $key, mixed $value, int $order, array $keys): array
{ {
if (is_null($value)) { if (is_null($value)) {
return [$keys, $params]; return $keys;
} }
if ( if (
str_starts_with($value, '+ ') || str_starts_with($value, '+ ') ||
@@ -190,10 +183,10 @@ class SqlBuilder extends Component
) { ) {
$keys[] = $key . '=' . $key . ' ' . $value; $keys[] = $key . '=' . $key . ' ' . $value;
} else { } else {
$params[':update' . $key . $order] = $value; $this->query->bindParam(':update' . $key . $order, $value);
$keys[] = $key . '=:update' . $key . $order; $keys[] = $key . '=:update' . $key . $order;
} }
return [$keys, $params]; return $keys;
} }
@@ -266,7 +259,7 @@ class SqlBuilder extends Component
if (count($this->query->select) < 1) { if (count($this->query->select) < 1) {
$this->query->select = ['*']; $this->query->select = ['*'];
} }
$select = "SELECT " . implode(',', $this->query->select) . " FROM " . $this->tableName(); $select = "SELECT " . implode(',', $this->query->select) . " FROM " . $this->query->from;
if ($this->query->alias != "") { if ($this->query->alias != "") {
$select .= " AS " . $this->query->alias; $select .= " AS " . $this->query->alias;
} }
@@ -297,7 +290,7 @@ class SqlBuilder extends Component
*/ */
public function truncate(): string public function truncate(): string
{ {
return sprintf('TRUNCATE %s', $this->tableName()); return sprintf('TRUNCATE %s', $this->query->from);
} }
@@ -310,24 +303,4 @@ class SqlBuilder extends Component
return $this->where($this->query->where); return $this->where($this->query->where);
} }
/**
* @return string
* @throws Exception
*/
public function tableName(): string
{
if ($this->query->from === null) {
return $this->query->modelClass->getTable();
}
if ($this->query->from instanceof \Closure) {
return $this->query->from = '(' . $this->query->makeClosureFunction($this->query->from) . ')';
}
if ($this->query->from instanceof ActiveQuery) {
return $this->query->from = '(' . SqlBuilder::builder($this->query->from)->get($this->query->from) . ')';
} else {
return $this->query->from;
}
}
} }
+7 -13
View File
@@ -70,12 +70,12 @@ trait Builder
/** /**
* @param $group * @param string $group
* @return string * @return string
*/ */
private function builderGroup($group): string private function builderGroup(string $group): string
{ {
if (empty($group)) { if ($group != '') {
return ''; return '';
} }
return ' GROUP BY ' . $group; return ' GROUP BY ' . $group;
@@ -143,7 +143,8 @@ trait Builder
private function resolveCondition($field, $condition, $_tmp): string private function resolveCondition($field, $condition, $_tmp): string
{ {
if (is_string($field)) { if (is_string($field)) {
return $field . ' = \'' . $condition . '\''; $this->query->bindParam(':where' . $field, $condition);
return $field . ' = ' . ':where' . $field;
} else if (is_string($condition)) { } else if (is_string($condition)) {
return $condition; return $condition;
} else { } else {
@@ -192,9 +193,6 @@ trait Builder
} }
public array $params = [];
/** /**
* @param $condition * @param $condition
* @return array * @return array
@@ -203,12 +201,8 @@ trait Builder
{ {
$_array = []; $_array = [];
foreach ($condition as $key => $value) { foreach ($condition as $key => $value) {
if (!is_numeric($key)) { $this->query->bindParam(':hash' . $key, $value);
$this->query->bindParam(':' . $key, $value); $_array[] = $key . '=:hash' . $key;
$_array[] = $key . '=:' . $key;
} else {
$_array[] = $value;
}
} }
return $_array; return $_array;
} }
+10 -54
View File
@@ -34,7 +34,7 @@ trait QueryTrait
public int $offset = 0; public int $offset = 0;
public int $limit = 500; public int $limit = 500;
public string $group = ''; public string $group = '';
public string|Closure|ActiveQuery|null $from = null; public string $from = '';
public string $alias = 't1'; public string $alias = 't1';
public array $filter = []; public array $filter = [];
@@ -103,7 +103,7 @@ trait QueryTrait
*/ */
public function whereRaw(string $whereRaw): static public function whereRaw(string $whereRaw): static
{ {
if (empty($whereRaw)) { if ($whereRaw == '') {
return $this; return $this;
} }
$this->where[] = $whereRaw; $this->where[] = $whereRaw;
@@ -112,36 +112,10 @@ trait QueryTrait
/** /**
* @param string|array|Closure $condition * @param bool $bool
* @param string|array|Closure $condition1
* @param string|array|Closure $condition2
* @return $this
* @throws
*/
public function whereIf(string|array|Closure $condition, string|array|Closure $condition1, string|array|Closure $condition2): static
{
if (!is_string($condition)) {
$condition = $this->makeClosureFunction($condition);
}
if (!is_string($condition1)) {
$condition1 = $this->makeClosureFunction($condition1);
}
if (!is_string($condition2)) {
$condition2 = $this->makeClosureFunction($condition2);
}
$this->where[] = 'IF(' . $condition . ', ' . $condition1 . ', ' . $condition2 . ')';
return $this;
}
/**
* @param $bool
* @return $this * @return $this
*/ */
public function ifNotWhere($bool): static public function ifNotWhere(bool $bool): static
{ {
$this->ifNotWhere = $bool; $this->ifNotWhere = $bool;
return $this; return $this;
@@ -210,27 +184,6 @@ trait QueryTrait
return $this; return $this;
} }
/**
* @param array|Closure|string $columns
* @return $this
*/
public function filter(array|Closure|string $columns): static
{
if (!$columns) {
return $this;
}
if (is_callable($columns, TRUE)) {
return call_user_func($columns, $this);
}
if (is_string($columns)) {
$columns = explode(',', $columns);
}
if (!is_array($columns)) {
return $this;
}
$this->filter = $columns;
return $this;
}
/** /**
* @param string $alias * @param string $alias
@@ -252,6 +205,9 @@ trait QueryTrait
*/ */
public function from(string|Closure $tableName): static public function from(string|Closure $tableName): static
{ {
if ($tableName instanceof Closure) {
$tableName = call_user_func($tableName, $this->makeNewSqlGenerate());
}
$this->from = $tableName; $this->from = $tableName;
return $this; return $this;
} }
@@ -325,7 +281,7 @@ trait QueryTrait
} }
$tableName = $model->getTable(); $tableName = $model->getTable();
} }
return $this->join(...["LEFT JOIN " . $tableName, $alias, $onCondition, $param]); return $this->join("LEFT JOIN " . $tableName, $alias, $onCondition, $param);
} }
/** /**
@@ -345,7 +301,7 @@ trait QueryTrait
} }
$tableName = $model->getTable(); $tableName = $model->getTable();
} }
return $this->join(...["RIGHT JOIN " . $tableName, $alias, $onCondition, $param]); return $this->join("RIGHT JOIN " . $tableName, $alias, $onCondition, $param);
} }
/** /**
@@ -365,7 +321,7 @@ trait QueryTrait
} }
$tableName = $model->getTable(); $tableName = $model->getTable();
} }
return $this->join(...["INNER JOIN " . $tableName, $alias, $onCondition, $param]); return $this->join("INNER JOIN " . $tableName, $alias, $onCondition, $param);
} }
/** /**