This commit is contained in:
2021-09-29 17:54:48 +08:00
parent 5ec3ca65e1
commit 2bd723775e
3 changed files with 37 additions and 13 deletions
+18 -12
View File
@@ -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
View File
@@ -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;
} }
+14
View File
@@ -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