This commit is contained in:
2020-11-17 18:48:26 +08:00
parent d7f3d4d65f
commit 88d7d5100a
+356 -353
View File
@@ -30,406 +30,409 @@ defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a
class ActiveRecord extends BaseActiveRecord class ActiveRecord extends BaseActiveRecord
{ {
const DECR = 'decr'; const DECR = 'decr';
const INCR = 'incr'; const INCR = 'incr';
/** /**
* @return array * @return array
*/ */
public function rules() public function rules()
{ {
return []; return [];
} }
/** /**
* @param string $column * @param string $column
* @param int $value * @param int $value
* @return ActiveRecord|false * @return ActiveRecord|false
* @throws Exception * @throws Exception
*/ */
public function increment(string $column, int $value) public function increment(string $column, int $value)
{ {
if (!$this->mathematics(self::INCR, [$column => $value])) { if (!$this->mathematics(self::INCR, [$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 ActiveRecord|false * @return ActiveRecord|false
* @throws Exception * @throws Exception
*/ */
public function decrement(string $column, int $value) public function decrement(string $column, int $value)
{ {
if (!$this->mathematics(self::DECR, [$column => $value])) { if (!$this->mathematics(self::DECR, [$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 ActiveRecord|false * @return ActiveRecord|false
* @throws Exception * @throws Exception
*/ */
public function increments(array $columns) public function increments(array $columns)
{ {
if (!$this->mathematics(self::INCR, $columns)) { if (!$this->mathematics(self::INCR, $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 ActiveRecord|false * @return ActiveRecord|false
* @throws Exception * @throws Exception
*/ */
public function decrements(array $columns) public function decrements(array $columns)
{ {
if (!$this->mathematics(self::DECR, $columns)) { if (!$this->mathematics(self::DECR, $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 $attributes * @param $attributes
* @param $condition * @param $condition
* @return bool|ActiveRecord|mixed * @return bool|ActiveRecord|mixed
* @throws Exception * @throws Exception
*/ */
public static function findOrCreate(array $condition, array $attributes = []) public static function findOrCreate(array $condition, array $attributes = [])
{ {
$select = static::find()->where($condition)->first(); $select = static::find()->where($condition)->first();
if (!empty($select)) { if (!empty($select)) {
return $select; return $select;
} }
if (empty($attributes)) { if (empty($attributes)) {
return \logger()->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); return \logger()->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
} }
$select = new static(); $select = new static();
$select->attributes = $attributes; $select->attributes = $attributes;
return $select->save(); if ($select->save()) {
} throw new Exception($select->getLastError());
}
return $select;
}
/** /**
* @param $action * @param $action
* @param $columns * @param $columns
* @param array $condition * @param array $condition
* @return array|bool|int|string|null * @return array|bool|int|string|null
* @throws Exception * @throws Exception
*/ */
private function mathematics($action, $columns, $condition = []) private function mathematics($action, $columns, $condition = [])
{ {
if (empty($condition)) { if (empty($condition)) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()]; $condition = [$this->getPrimary() => $this->getPrimaryValue()];
} }
return static::getDb()->createCommand() return static::getDb()->createCommand()
->mathematics(self::getTable(), [$action => $columns], $condition) ->mathematics(self::getTable(), [$action => $columns], $condition)
->exec(); ->exec();
} }
/** /**
* @param $column * @param $column
* @param $value * @param $value
* @param array $condition * @param array $condition
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public static function incrAll($column, $value, $condition = []) public static function incrAll($column, $value, $condition = [])
{ {
return static::getDb()->createCommand() return static::getDb()->createCommand()
->mathematics(self::getTable(), [self::INCR => [$column, $value]], $condition) ->mathematics(self::getTable(), [self::INCR => [$column, $value]], $condition)
->exec(); ->exec();
} }
/** /**
* @param $column * @param $column
* @param $value * @param $value
* @param array $condition * @param array $condition
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public static function decrAll($column, $value, $condition = []) public static function decrAll($column, $value, $condition = [])
{ {
return static::getDb()->createCommand() return static::getDb()->createCommand()
->mathematics(self::getTable(), [self::DECR => [$column, $value]], $condition) ->mathematics(self::getTable(), [self::DECR => [$column, $value]], $condition)
->exec(); ->exec();
} }
/** /**
* @param array $attributes * @param array $attributes
* @return bool * @return bool
* @throws * @throws
*/ */
public function update(array $attributes) public function update(array $attributes)
{ {
return $this->save($attributes); return $this->save($attributes);
} }
/** /**
* @param array $params * @param array $params
* @param array $condition * @param array $condition
* @return bool|static * @return bool|static
* @throws Exception * @throws Exception
*/ */
public static function insertOrUpdate(array $params, array $condition) public static function insertOrUpdate(array $params, array $condition)
{ {
$first = static::findOrCreate($params, $condition); $first = static::findOrCreate($params, $condition);
$first->attributes = $params; $first->attributes = $params;
if (!$first->save()) { if (!$first->save()) {
return false; return false;
} }
return $first; return $first;
} }
/** /**
* @param array $data * @param array $data
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public static function batchInsert(array $data): bool public static function batchInsert(array $data): bool
{ {
/** @var static $class */ /** @var static $class */
$class = Snowflake::createObject(static::className()); $class = Snowflake::createObject(static::className());
if (empty($data)) { if (empty($data)) {
return $class->addError('Insert data empty.', 'mysql'); return $class->addError('Insert data empty.', 'mysql');
} }
return $class::find()->batchInsert($data); return $class::find()->batchInsert($data);
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function delete() public function delete()
{ {
$conditions = $this->_oldAttributes; $conditions = $this->_oldAttributes;
if (empty($conditions)) { if (empty($conditions)) {
return $this->addError("Delete condition do not empty.", 'mysql'); return $this->addError("Delete condition do not empty.", 'mysql');
} }
$primary = $this->getPrimary(); $primary = $this->getPrimary();
if (!empty($primary)) { if (!empty($primary)) {
$sul = static::deleteAll([$primary => $this->getAttribute($primary)]); $sul = static::deleteAll([$primary => $this->getAttribute($primary)]);
} else { } else {
$sul = static::deleteAll($conditions); $sul = static::deleteAll($conditions);
} }
if (!$sul) { if (!$sul) {
return false; return false;
} }
if (method_exists($this, 'afterDelete')) { if (method_exists($this, 'afterDelete')) {
$this->afterDelete(); $this->afterDelete();
} }
return true; return true;
} }
/** /**
* @param $condition * @param $condition
* @param array $attributes * @param array $attributes
* *
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public static function batchUpdate($condition, $attributes = []) public static function batchUpdate($condition, $attributes = [])
{ {
$condition = static::find()->where($condition); $condition = static::find()->where($condition);
return $condition->batchUpdate($attributes); return $condition->batchUpdate($attributes);
} }
/** /**
* @param $condition * @param $condition
* @param array $attributes * @param array $attributes
* *
* @return array|mixed|null|Collection * @return array|mixed|null|Collection
* @throws Exception * @throws Exception
*/ */
public static function findAll($condition, $attributes = []) public static function findAll($condition, $attributes = [])
{ {
$query = static::find()->where($condition); $query = static::find()->where($condition);
if (!empty($attributes)) { if (!empty($attributes)) {
$query->bindParams($attributes); $query->bindParams($attributes);
} }
return $query->all(); return $query->all();
} }
/** /**
* @param $data * @param $data
* @return array|mixed * @return array|mixed
* @throws Exception * @throws Exception
*/ */
private function resolveObject($data) private function resolveObject($data)
{ {
if (is_numeric($data) || !is_string($data)) { if (is_numeric($data) || !is_string($data)) {
return $data; return $data;
} }
if (!method_exists($this, $data)) { if (!method_exists($this, $data)) {
return $data; return $data;
} }
return ArrayAccess::toArray($this->{$data}()); return ArrayAccess::toArray($this->{$data}());
} }
/** /**
* @return array|mixed * @return array|mixed
* @throws Exception * @throws Exception
*/ */
public function toArray() public function toArray()
{ {
$data = []; $data = [];
foreach ($this->_attributes as $key => $val) { foreach ($this->_attributes as $key => $val) {
$data[$key] = $this->getAttribute($key); $data[$key] = $this->getAttribute($key);
} }
return array_merge($data, $this->runRelate()); return array_merge($data, $this->runRelate());
} }
/** /**
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
private function runRelate() private function runRelate()
{ {
$relates = []; $relates = [];
if (empty($this->_relate)) { if (empty($this->_relate)) {
return $relates; return $relates;
} }
foreach ($this->_relate as $key => $val) { foreach ($this->_relate as $key => $val) {
$relates[$key] = $this->resolveObject($val); $relates[$key] = $this->resolveObject($val);
} }
return $relates; return $relates;
} }
/** /**
* @param $modelName * @param $modelName
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return mixed|ActiveQuery * @return mixed|ActiveQuery
* @throws Exception * @throws Exception
*/ */
public function hasOne(string $modelName, $foreignKey, $localKey) public function hasOne(string $modelName, $foreignKey, $localKey)
{ {
if (!$this->has($localKey)) { if (!$this->has($localKey)) {
throw new Exception("Need join table primary key."); throw new Exception("Need join table primary key.");
} }
$value = $this->getAttribute($localKey); $value = $this->getAttribute($localKey);
$relation = $this->getRelation(); $relation = $this->getRelation();
return new HasOne($modelName, $foreignKey, $value, $relation); return new HasOne($modelName, $foreignKey, $value, $relation);
} }
/** /**
* @param $modelName * @param $modelName
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return mixed|ActiveQuery * @return mixed|ActiveQuery
* @throws Exception * @throws Exception
*/ */
public function hasCount($modelName, $foreignKey, $localKey) public function hasCount($modelName, $foreignKey, $localKey)
{ {
if (!$this->has($localKey)) { if (!$this->has($localKey)) {
throw new Exception("Need join table primary key."); throw new Exception("Need join table primary key.");
} }
$value = $this->getAttribute($localKey); $value = $this->getAttribute($localKey);
$relation = $this->getRelation(); $relation = $this->getRelation();
return new HasCount($modelName, $foreignKey, $value, $relation); return new HasCount($modelName, $foreignKey, $value, $relation);
} }
/** /**
* @param $modelName * @param $modelName
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return mixed|ActiveQuery * @return mixed|ActiveQuery
* @throws Exception * @throws Exception
*/ */
public function hasMany($modelName, $foreignKey, $localKey) public function hasMany($modelName, $foreignKey, $localKey)
{ {
if (!$this->has($localKey)) { if (!$this->has($localKey)) {
throw new Exception("Need join table primary key."); throw new Exception("Need join table primary key.");
} }
$value = $this->getAttribute($localKey); $value = $this->getAttribute($localKey);
$relation = $this->getRelation(); $relation = $this->getRelation();
return new HasMany($modelName, $foreignKey, $value, $relation); return new HasMany($modelName, $foreignKey, $value, $relation);
} }
/** /**
* @param $modelName * @param $modelName
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return mixed|ActiveQuery * @return mixed|ActiveQuery
* @throws Exception * @throws Exception
*/ */
public function hasIn($modelName, $foreignKey, $localKey) public function hasIn($modelName, $foreignKey, $localKey)
{ {
if (!$this->has($localKey)) { if (!$this->has($localKey)) {
throw new Exception("Need join table primary key."); throw new Exception("Need join table primary key.");
} }
$value = $this->getAttribute($localKey); $value = $this->getAttribute($localKey);
$relation = $this->getRelation(); $relation = $this->getRelation();
return new HasMany($modelName, $foreignKey, $value, $relation); return new HasMany($modelName, $foreignKey, $value, $relation);
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function afterDelete() public function afterDelete()
{ {
if (!$this->hasPrimary()) { if (!$this->hasPrimary()) {
return TRUE; return TRUE;
} }
$value = $this->getPrimaryValue(); $value = $this->getPrimaryValue();
if (empty($value)) { if (empty($value)) {
return TRUE; return TRUE;
} }
return TRUE; return TRUE;
} }
/** /**
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function beforeDelete() public function beforeDelete()
{ {
if (!$this->hasPrimary()) { if (!$this->hasPrimary()) {
return TRUE; return TRUE;
} }
$value = $this->getPrimaryValue(); $value = $this->getPrimaryValue();
if (empty($value)) { if (empty($value)) {
return TRUE; return TRUE;
} }
return TRUE; return TRUE;
} }
} }