This commit is contained in:
xl
2023-07-10 11:28:53 +08:00
parent 203eda17ad
commit 9d7605b8e4
2 changed files with 308 additions and 306 deletions
+307 -305
View File
@@ -34,355 +34,357 @@ class Model extends Base\Model
{ {
/** /**
* @param string $column * @param string $column
* @param int $value * @param int $value
* @return ModelInterface|false * @return ModelInterface|false
* @throws Exception * @throws Exception
*/ */
public function increment(string $column, int $value): bool|ModelInterface public function increment(string $column, int $value): bool|ModelInterface
{ {
if (!$this->mathematics([$column => $value], '+')) { if (!$this->mathematics([$column => $value], '+')) {
return false; return false;
} }
$this->{$column} += $value; $this->{$column} += $value;
return $this->refresh(); return $this->refresh();
} }
/** /**
* @param string $column * @param string $column
* @param int $value * @param int $value
* @return ModelInterface|false * @return ModelInterface|false
* @throws Exception * @throws Exception
*/ */
public function decrement(string $column, int $value): bool|ModelInterface public function decrement(string $column, int $value): bool|ModelInterface
{ {
if (!$this->mathematics([$column => $value], '-')) { if (!$this->mathematics([$column => $value], '-')) {
return false; return false;
} }
$this->{$column} -= $value; $this->{$column} -= $value;
return $this->refresh(); return $this->refresh();
} }
/** /**
* @param array $columns * @param array $columns
* @return ModelInterface|false * @return ModelInterface|false
* @throws Exception * @throws Exception
*/ */
public function increments(array $columns): bool|static public function increments(array $columns): bool|static
{ {
if (!$this->mathematics($columns, '+')) { if (!$this->mathematics($columns, '+')) {
return false; return false;
} }
foreach ($columns as $key => $attribute) { foreach ($columns as $key => $attribute) {
$this->$key += $attribute; $this->$key += $attribute;
} }
return $this; return $this;
} }
/** /**
* @param array $columns * @param array $columns
* @return ModelInterface|false * @return ModelInterface|false
* @throws Exception * @throws Exception
*/ */
public function decrements(array $columns): bool|static public function decrements(array $columns): bool|static
{ {
if (!$this->mathematics($columns, '-')) { if (!$this->mathematics($columns, '-')) {
return false; return false;
} }
foreach ($columns as $key => $attribute) { foreach ($columns as $key => $attribute) {
$this->$key -= $attribute; $this->$key -= $attribute;
} }
return $this; return $this;
} }
/** /**
* @param array $condition * @param array $condition
* @param array $attributes * @param array $attributes
* @return bool|static * @return bool|static
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public static function findOrCreate(array $condition, array $attributes): bool|static public static function findOrCreate(array $condition, array $attributes): bool|static
{ {
return Db::Transaction(function ($condition, $attributes) { return Db::Transaction(function ($condition, $attributes) {
/** @var static $select */ /** @var static $select */
$select = static::query()->where($condition)->first(); $select = static::query()->where($condition)->first();
if ($select === null) { if ($select === null) {
$select = static::populate(array_merge($condition, $attributes))->create(); $select = static::populate(array_merge($condition, $attributes))->create();
} }
return $select; return $select;
}, $condition, $attributes); }, $condition, $attributes);
} }
/** /**
* @param array $condition * @param array $condition
* @param array $attributes * @param array $attributes
* @return bool|static * @return bool|static
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public static function createOrUpdate(array $condition, array $attributes = []): bool|static public static function createOrUpdate(array $condition, array $attributes = []): bool|static
{ {
return Db::Transaction(function ($condition, $attributes) { return Db::Transaction(function ($condition, $attributes) {
/** @var static $select */ /** @var static $select */
$select = static::query()->where($condition)->first(); $select = static::query()->where($condition)->first();
if (empty($select)) { if (empty($select)) {
$select = static::populate($condition); $select = static::populate($condition);
} }
return $select->save($attributes); return $select->save($attributes);
}, $condition, $attributes); }, $condition, $attributes);
} }
/** /**
* @param $columns * @param $columns
* @param $action * @param $action
* @return array|bool|int|string|null * @return array|bool|int|string|null
* @throws Exception * @throws Exception
*/ */
private function mathematics($columns, $action): int|bool|array|string|null private function mathematics($columns, $action): int|bool|array|string|null
{ {
$condition = [$this->getPrimary() => $this->getPrimaryValue()]; $condition = [$this->getPrimary() => $this->getPrimaryValue()];
$activeQuery = static::query()->where($condition); $activeQuery = static::query()->where($condition);
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action); $create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
if (is_bool($create)) { if (is_bool($create)) {
return false; return false;
} }
return $this->getConnection()->createCommand($create, $activeQuery->attributes)->exec(); return $this->getConnection()->createCommand($create, $activeQuery->attributes)->exec();
} }
/** /**
* @param array $params * @param array $params
* @return ModelInterface|bool * @return ModelInterface|bool
* @throws Exception * @throws Exception
*/ */
public function update(array $params): static|bool public function update(array $params): static|bool
{ {
if (!$this->validator($this->rules()) || !$this->beforeSave($this)) { if (!$this->validator($this->rules()) || !$this->beforeSave($this)) {
return FALSE; return FALSE;
} }
$condition = array_diff_assoc($this->_oldAttributes, $params); $condition = array_diff_assoc($this->_oldAttributes, $params);
$old = array_intersect_key($this->_oldAttributes, $params); $old = array_intersect_key($this->_oldAttributes, $params);
return $this->updateInternal($old, $condition, $params); return $this->updateInternal($old, $condition, $params);
} }
/** /**
* @param array $data * @param array $data
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public static function inserts(array $data): bool public static function inserts(array $data): bool
{ {
if (empty($data)) { if (empty($data)) {
return addError('Insert data empty.', 'mysql'); return addError('Insert data empty.', 'mysql');
} }
return static::query()->insert($data); return static::query()->insert($data);
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function delete(): bool public function delete(): bool
{ {
if ($this->beforeDelete()) { if (!$this->beforeDelete()) {
$result = static::deleteByCondition($this->_attributes, $this->_attributes); return false;
}
return $this->afterDelete($result); if ($this->hasPrimary()) {
} else { $result = static::deleteByCondition("id = :id", [":id" => $this->getPrimaryValue()]);
return false; } else {
} $result = static::deleteByCondition($this->_attributes);
} }
return $this->afterDelete($result);
}
/** /**
* @param mixed $condition * @param mixed $condition
* @param array $attributes * @param array $attributes
* *
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public static function updateAll(mixed $condition, array $attributes = []): bool public static function updateAll(mixed $condition, array $attributes = []): bool
{ {
$condition = static::query()->where($condition); $condition = static::query()->where($condition);
return $condition->update($attributes); return $condition->update($attributes);
} }
/** /**
* @param $condition * @param $condition
* @return array|Collection * @return array|Collection
* @throws Exception * @throws Exception
*/ */
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)->all();
} }
/** /**
* @param $condition * @param $condition
* @param array $attributes * @param array $attributes
* *
* @return array|Collection * @return array|Collection
* @throws Exception * @throws Exception
*/ */
public static function findAll($condition, array $attributes = []): array|Collection public static function findAll($condition, array $attributes = []): array|Collection
{ {
$query = static::query()->where($condition); $query = static::query()->where($condition);
if (!empty($attributes)) { if (!empty($attributes)) {
$query->bindParams($attributes); $query->bindParams($attributes);
} }
return $query->all(); return $query->all();
} }
/** /**
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public function toArray(): array public function toArray(): array
{ {
$data = $this->_attributes; $data = $this->_attributes;
foreach ($data as $key => $datum) { foreach ($data as $key => $datum) {
$method = 'get' . ucfirst($key) . 'Attribute'; $method = 'get' . ucfirst($key) . 'Attribute';
if (!method_exists($this, $method)) { if (!method_exists($this, $method)) {
continue; continue;
} }
$data[$key] = $this->{$method}($datum); $data[$key] = $this->{$method}($datum);
} }
return $this->withs($data); return $this->withs($data);
} }
/** /**
* @param $data * @param $data
* @return array * @return array
*/ */
private function withs($data): array private function withs($data): array
{ {
$with = $this->getWith(); $with = $this->getWith();
foreach ($with as $value) { foreach ($with as $value) {
$join = $this->withRelate($value); $join = $this->withRelate($value);
if ($join instanceof Kiri\ToArray) { if ($join instanceof Kiri\ToArray) {
$join = $join->toArray(); $join = $join->toArray();
} }
$data[$value] = $join; $data[$value] = $join;
} }
return $data; return $data;
} }
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function _hasBase(ModelInterface|string $modelName, $foreignKey, $localKey): string private function _hasBase(ModelInterface|string $modelName, $foreignKey, $localKey): string
{ {
if (($value = $this->{$localKey}) === null) { if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key."); throw new Exception("Need join table primary key.");
} }
$relation = $this->getRelation(); $relation = $this->getRelation();
$primaryKey = $modelName . $foreignKey . $value; $primaryKey = $modelName . $foreignKey . $value;
if (!$relation->hasIdentification($primaryKey)) { if (!$relation->hasIdentification($primaryKey)) {
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value])); $relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
} }
return $primaryKey; return $primaryKey;
} }
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return HasOne|ActiveQuery * @return HasOne|ActiveQuery
* @throws Exception * @throws Exception
*/ */
public function hasOne(ModelInterface|string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery public function hasOne(ModelInterface|string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
{ {
return new HasOne($this->_hasBase($modelName, $foreignKey, $localKey)); return new HasOne($this->_hasBase($modelName, $foreignKey, $localKey));
} }
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return ActiveQuery|HasCount * @return ActiveQuery|HasCount
* @throws Exception * @throws Exception
*/ */
public function hasCount(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasCount public function hasCount(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasCount
{ {
return new HasCount($this->_hasBase($modelName, $foreignKey, $localKey)); return new HasCount($this->_hasBase($modelName, $foreignKey, $localKey));
} }
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return ActiveQuery|HasMany * @return ActiveQuery|HasMany
* @throws Exception * @throws Exception
*/ */
public function hasMany(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany public function hasMany(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany
{ {
return new HasMany($this->_hasBase($modelName, $foreignKey, $localKey)); return new HasMany($this->_hasBase($modelName, $foreignKey, $localKey));
} }
/** /**
* @param ModelInterface|string $modelName * @param ModelInterface|string $modelName
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return ActiveQuery|HasMany * @return ActiveQuery|HasMany
* @throws Exception * @throws Exception
*/ */
public function hasIn(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany public function hasIn(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany
{ {
if (($value = $this->{$localKey}) === null) { if (($value = $this->{$localKey}) === null) {
throw new Exception("Need join table primary key."); throw new Exception("Need join table primary key.");
} }
$relation = $this->getRelation(); $relation = $this->getRelation();
$primaryKey = $modelName . $foreignKey . json_encode($value, JSON_UNESCAPED_UNICODE); $primaryKey = $modelName . $foreignKey . json_encode($value, JSON_UNESCAPED_UNICODE);
if (!$relation->hasIdentification($primaryKey)) { if (!$relation->hasIdentification($primaryKey)) {
$relation->bindIdentification($primaryKey, $modelName::query()->whereIn($foreignKey, $value)); $relation->bindIdentification($primaryKey, $modelName::query()->whereIn($foreignKey, $value));
} }
return new HasMany($primaryKey); return new HasMany($primaryKey);
} }
/** /**
* @param bool $result * @param bool $result
* @return bool * @return bool
*/ */
public function afterDelete(bool $result): bool public function afterDelete(bool $result): bool
{ {
return $result; return $result;
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function beforeDelete(): bool public function beforeDelete(): bool
{ {
return TRUE; return TRUE;
} }
} }
+1 -1
View File
@@ -731,7 +731,7 @@ trait QueryTrait
if ($params === null) { if ($params === null) {
return $this; return $this;
} }
$this->attributes = $params; $this->attributes = array_merge($this->attributes, $params);
return $this; return $this;
} }