From e89062a2d70b3bd9f0978e3cbf7efec64000f151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 18 Jan 2022 18:06:24 +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 | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Model.php b/Model.php index d6056c8..ef316d4 100644 --- a/Model.php +++ b/Model.php @@ -13,8 +13,8 @@ namespace Database; use Database\Base\Getter; use Database\Traits\HasBase; use Exception; -use Kiri\Exception\NotFindClassException; use Kiri; +use Kiri\Exception\NotFindClassException; use Kiri\ToArray; use ReflectionException; use Swoole\Coroutine; @@ -113,24 +113,23 @@ class Model extends Base\Model return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); } Db::beginTransaction(); - - /** @var static $select */ - $select = static::query()->where($condition)->first(); - var_dump($select); - if (empty($select)) { - $select = new static(); - $select->attributes = $attributes; - if (!$select->save()) { - var_dump('end rollback'); - Db::rollback(); - return $logger->addError($select->getLastError(), 'mysql'); + try { + /** @var static $select */ + $select = static::query()->where($condition)->first(); + if (empty($select)) { + $select = new static(); + $select->attributes = $attributes; + if (!$select->save()) { + throw new Exception($select->getLastError()); + } } + Db::commit(); + + return $select; + } catch (\Throwable $throwable) { + Db::rollback(); + return $logger->addError($throwable->getMessage(), 'mysql'); } - - var_dump('end commit'); - - Db::commit(); - return $select; }