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