This commit is contained in:
2021-03-31 01:58:36 +08:00
parent 1705d3d3f8
commit 5275d593b0
+7 -4
View File
@@ -121,18 +121,20 @@ class ActiveRecord extends BaseActiveRecord
*/ */
public static function findOrCreate(array $condition, array $attributes = []): bool|static public static function findOrCreate(array $condition, array $attributes = []): bool|static
{ {
$logger = Snowflake::app()->getLogger();
/** @var static $select */ /** @var static $select */
$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 = self::getModelClass(); $select = self::getModelClass();
$select->attributes = $attributes; $select->attributes = $attributes;
if (!$select->save()) { if (!$select->save()) {
return \logger()->addError($select->getLastError(), 'mysql'); return $logger->addError($select->getLastError(), 'mysql');
} }
return $select; return $select;
} }
@@ -146,8 +148,9 @@ class ActiveRecord extends BaseActiveRecord
*/ */
public static function createOrUpdate(array $condition, array $attributes = []): bool|static public static function createOrUpdate(array $condition, array $attributes = []): bool|static
{ {
$logger = Snowflake::app()->getLogger();
if (empty($attributes)) { if (empty($attributes)) {
return \logger()->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
} }
/** @var static $select */ /** @var static $select */
$select = static::find()->where($condition)->first(); $select = static::find()->where($condition)->first();
@@ -156,7 +159,7 @@ class ActiveRecord extends BaseActiveRecord
} }
$select->attributes = $attributes; $select->attributes = $attributes;
if (!$select->save()) { if (!$select->save()) {
return \logger()->addError($select->getLastError(), 'mysql'); return $logger->addError($select->getLastError(), 'mysql');
} }
return $select; return $select;
} }