改名
This commit is contained in:
+18
-12
@@ -105,23 +105,24 @@ class Model extends Base\Model
|
|||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function findOrCreate(array $condition, array $attributes = []): bool|static
|
public static function findOrCreate(array $condition, array $attributes): bool|static
|
||||||
{
|
{
|
||||||
$logger = Kiri::app()->getLogger();
|
$logger = Kiri::app()->getLogger();
|
||||||
|
|
||||||
/** @var static $select */
|
|
||||||
$select = static::query()->where($condition)->first();
|
|
||||||
if (!empty($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 = duplicate(static::class);
|
Db::beginTransaction();
|
||||||
$select->attributes = $attributes;
|
/** @var static $select */
|
||||||
if (!$select->save()) {
|
$select = static::query()->where($condition)->first();
|
||||||
return $logger->addError($select->getLastError(), 'mysql');
|
if (empty($select)) {
|
||||||
|
$select = duplicate(static::class);
|
||||||
|
$select->attributes = $attributes;
|
||||||
|
if (!$select->save()) {
|
||||||
|
Db::rollback();
|
||||||
|
return $logger->addError($select->getLastError(), 'mysql');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Db::commit();
|
||||||
return $select;
|
return $select;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +139,8 @@ class Model extends Base\Model
|
|||||||
if (empty($attributes)) {
|
if (empty($attributes)) {
|
||||||
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Db::beginTransaction();
|
||||||
/** @var static $select */
|
/** @var static $select */
|
||||||
$select = static::query()->where($condition)->first();
|
$select = static::query()->where($condition)->first();
|
||||||
if (empty($select)) {
|
if (empty($select)) {
|
||||||
@@ -145,7 +148,10 @@ class Model extends Base\Model
|
|||||||
}
|
}
|
||||||
$select->attributes = $attributes;
|
$select->attributes = $attributes;
|
||||||
if (!$select->save()) {
|
if (!$select->save()) {
|
||||||
return $logger->addError($select->getLastError(), 'mysql');
|
Db::rollback();
|
||||||
|
$select = $logger->addError($select->getLastError(), 'mysql');
|
||||||
|
} else {
|
||||||
|
Db::commit();
|
||||||
}
|
}
|
||||||
return $select;
|
return $select;
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-1
@@ -276,7 +276,11 @@ class SqlBuilder extends Component
|
|||||||
if ($hasOrder === true && !empty($this->query->order)) {
|
if ($hasOrder === true && !empty($this->query->order)) {
|
||||||
$select .= $this->builderOrder($this->query->order);
|
$select .= $this->builderOrder($this->query->order);
|
||||||
}
|
}
|
||||||
return $select . $this->builderLimit($this->query);
|
$sql = $select . $this->builderLimit($this->query);
|
||||||
|
if ($this->query->lock) {
|
||||||
|
$sql .= ' FOR UPDATE';
|
||||||
|
}
|
||||||
|
return $sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,9 @@ trait QueryTrait
|
|||||||
public string $alias = 't1';
|
public string $alias = 't1';
|
||||||
public array $filter = [];
|
public array $filter = [];
|
||||||
|
|
||||||
|
|
||||||
|
public bool $lock = false;
|
||||||
|
|
||||||
public bool $ifNotWhere = false;
|
public bool $ifNotWhere = false;
|
||||||
|
|
||||||
|
|
||||||
@@ -83,6 +86,17 @@ trait QueryTrait
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $lock
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function lock(bool $lock): static
|
||||||
|
{
|
||||||
|
$this->lock = $lock;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $whereRaw
|
* @param string $whereRaw
|
||||||
* @return QueryTrait
|
* @return QueryTrait
|
||||||
|
|||||||
Reference in New Issue
Block a user