From 703300182b3efd8a6486aa5a0464da5d69214deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Fri, 25 Aug 2023 10:01:53 +0800 Subject: [PATCH] qqq --- Base/Model.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Base/Model.php b/Base/Model.php index 1f5414a..0f62c5d 100644 --- a/Base/Model.php +++ b/Base/Model.php @@ -434,16 +434,20 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T { [$sql, $param] = SqlBuilder::builder(static::query())->insert($this->_attributes); - $dbConnection = $this->getConnection()->createCommand($sql, $param); + $connection = $this->getConnection()->beginTransaction(); + $dbConnection = $connection->createCommand($sql, $param); $lastId = $dbConnection->save(); if ($lastId === false) { + $connection->rollback(); return false; + } else { + $connection->commit(); + if ($this->hasPrimary()) { + $this->_attributes[$this->getPrimary()] = $lastId; + } + return $this; } - if ($this->hasPrimary()) { - $this->_attributes[$this->getPrimary()] = $lastId; - } - return $this; }