From 164065493e767f573b4205706cf21effad2b909c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 18 Jan 2022 18:53:54 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E6=94=B9=E5=90=8D"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit fdf58326 --- Model.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Model.php b/Model.php index c34c0eb..d7c954a 100644 --- a/Model.php +++ b/Model.php @@ -112,18 +112,29 @@ class Model extends Base\Model if (empty($attributes)) { return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); } + $model = new static(); + + /** @var Connection $database */ + $database = Kiri::app()->get($model->getConnection()); + $database->beginTransaction(); try { /** @var static $select */ $select = static::query()->where($condition)->first(); if (empty($select)) { - $select = new static(); + $select = $model; $select->attributes = $attributes; if (!$select->save()) { throw new Exception($select->getLastError()); } } + + $database->commit(); + return $select; } catch (\Throwable $throwable) { + + $database->rollback(); + return $logger->addError($throwable->getMessage(), 'mysql'); } }