From 51efe678e7e5383257a244b2215a64d6adc984f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sat, 13 Feb 2021 17:05:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/Base/BaseActiveRecord.php | 31 +++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 7b432071..a1663fec 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -401,16 +401,8 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess if (($lastId = $commandExec->save(true, $this->hasAutoIncrement())) === false) { throw new Exception('保存失败.' . $sqlBuilder); } - if ($this->hasAutoIncrement()) { - $this->setAttribute($this->getAutoIncrement(), (int)$lastId); - } else if ($this->hasPrimary()) { - $primary = $this->getPrimary(); - if (!isset($param[$primary]) || empty($param[$primary])) { - $this->setAttribute($primary, (int)$lastId); - } - } $trance->commit(); - $this->setAttributes($param); + $this->setPrimary($lastId, $param); $this->afterSave($attributes, $param); $lastId = $this->refresh(); } catch (\Throwable $exception) { @@ -421,6 +413,27 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess } + /** + * @param $lastId + * @param $param + * @return mixed + * @throws Exception + */ + private function setPrimary($lastId, $param) + { + if ($this->hasAutoIncrement()) { + return $this->setAttribute($this->getAutoIncrement(), (int)$lastId); + } + if ($this->hasPrimary()) { + $primary = $this->getPrimary(); + if (!isset($param[$primary]) || empty($param[$primary])) { + $this->setAttribute($primary, (int)$lastId); + } + } + return $this->setAttributes($param); + } + + /** * @param $param * @param $condition