This commit is contained in:
2023-09-01 10:53:38 +08:00
parent 0bff75446d
commit 2ee482241d
2 changed files with 4 additions and 8 deletions
+2 -6
View File
@@ -434,15 +434,13 @@ 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);
$connection = $this->getConnection()->beginTransaction(); $connection = $this->getConnection();
$dbConnection = $connection->createCommand($sql, $param); $dbConnection = $connection->createCommand($sql, $param);
$lastId = $dbConnection->save(); $lastId = $dbConnection->save();
if ($lastId === false) { if ($lastId === false) {
$connection->rollback();
return false; return false;
} else { } else {
$connection->commit();
if ($this->hasPrimary()) { if ($this->hasPrimary()) {
$this->_attributes[$this->getPrimary()] = $lastId; $this->_attributes[$this->getPrimary()] = $lastId;
} }
@@ -469,13 +467,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return false; return false;
} }
$connection = $this->getConnection()->beginTransaction(); $connection = $this->getConnection();
$command = $connection->createCommand($generate, $query->attributes); $command = $connection->createCommand($generate, $query->attributes);
if ($command->save()) { if ($command->save()) {
$connection->commit();
return $this->refresh()->afterSave($old, $change); return $this->refresh()->afterSave($old, $change);
} else { } else {
$connection->rollback();
return FALSE; return FALSE;
} }
} }
+2 -2
View File
@@ -282,7 +282,7 @@ class Connection extends Component
if ($pdo === null) { if ($pdo === null) {
throw new Exception('Failed to rollback transaction: connection was exists.'); throw new Exception('Failed to rollback transaction: connection was exists.');
} }
if ($pdo->inTransaction()) { if ($this->inTransaction()) {
$pdo->rollback(); $pdo->rollback();
} }
$this->release($pdo); $this->release($pdo);
@@ -301,7 +301,7 @@ class Connection extends Component
if ($pdo === null) { if ($pdo === null) {
throw new Exception('Failed to commit transaction: connection was exists.'); throw new Exception('Failed to commit transaction: connection was exists.');
} }
if ($pdo->inTransaction()) { if ($this->inTransaction()) {
$pdo->commit(); $pdo->commit();
} }
$this->release($pdo); $this->release($pdo);