diff --git a/Base/Model.php b/Base/Model.php index e920295..1f5414a 100644 --- a/Base/Model.php +++ b/Base/Model.php @@ -465,10 +465,13 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T return false; } - $command = $this->getConnection()->createCommand($generate, $query->attributes); + $connection = $this->getConnection()->beginTransaction(); + $command = $connection->createCommand($generate, $query->attributes); if ($command->save()) { + $connection->commit(); return $this->refresh()->afterSave($old, $change); } else { + $connection->rollback(); return FALSE; } } @@ -479,16 +482,15 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T */ public function save(): static|bool { + if (!$this->validator($this->rules()) || !$this->beforeSave($this)) { + return FALSE; + } if (!$this->isNewExample) { - if (!$this->validator($this->rules()) || !$this->beforeSave($this)) { - return FALSE; - } - [$changes, $condition] = $this->diff(); return $this->updateInternal($condition, $condition, $changes); } else { - return $this->create(); + return $this->insert(); } } @@ -509,19 +511,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } - /** - * @return $this|bool - * @throws Exception - */ - protected function create(): bool|static - { - if (!$this->validator($this->rules()) || !$this->beforeSave($this)) { - return FALSE; - } - return $this->insert(); - } - - /** * @param $value * @return $this diff --git a/Connection.php b/Connection.php index a8842ce..b62cf53 100644 --- a/Connection.php +++ b/Connection.php @@ -201,8 +201,8 @@ class Connection extends Component { if ($this->storey == 0) { /** @var PDO $pdo */ - $pdo = Context::get($this->cds); - if ($pdo instanceof PDO && !$this->inTransaction()) { + $pdo = Context::get($this->cds, $this->getTransactionClient()); + if (!$this->inTransaction()) { $pdo->beginTransaction(); } }