This commit is contained in:
2021-03-31 01:54:05 +08:00
parent 5e88b0a99c
commit f84b396049
+40 -9
View File
@@ -129,15 +129,7 @@ class ActiveRecord extends BaseActiveRecord
if (empty($attributes)) {
return \logger()->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
}
$className = get_called_class();
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
$select = $channel->pop($className, function () use ($className) {
return new $className();
});
$select = self::getModelClass();
$select->attributes = $attributes;
if (!$select->save()) {
return \logger()->addError($select->getLastError(), 'mysql');
@@ -146,6 +138,45 @@ class ActiveRecord extends BaseActiveRecord
}
/**
* @param array $condition
* @param array $attributes
* @return bool|static
* @throws Exception
*/
public static function updateOrCreate(array $condition, array $attributes = []): bool|static
{
if (empty($attributes)) {
return \logger()->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
}
/** @var static $select */
$select = static::find()->where($condition)->first();
if (empty($select)) {
$select = self::getModelClass();
}
$select->attributes = $attributes;
if (!$select->save()) {
return \logger()->addError($select->getLastError(), 'mysql');
}
return $select;
}
/**
* @return static
* @throws Exception
*/
private static function getModelClass(): static
{
$className = get_called_class();
/** @var Channel $channel */
$channel = Snowflake::app()->get('channel');
return $channel->pop($className, function () use ($className) {
return new $className();
});
}
/**
* @param $action
* @param $columns