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