This commit is contained in:
2023-08-25 10:01:53 +08:00
parent 5caed72909
commit 703300182b
+6 -2
View File
@@ -434,17 +434,21 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
[$sql, $param] = SqlBuilder::builder(static::query())->insert($this->_attributes); [$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(); $lastId = $dbConnection->save();
if ($lastId === false) { if ($lastId === false) {
$connection->rollback();
return false; return false;
} } else {
$connection->commit();
if ($this->hasPrimary()) { if ($this->hasPrimary()) {
$this->_attributes[$this->getPrimary()] = $lastId; $this->_attributes[$this->getPrimary()] = $lastId;
} }
return $this; return $this;
} }
}
/** /**