diff --git a/src/Adapter/Mysql.php b/src/Adapter/Mysql.php index a0986ba..5917f6f 100644 --- a/src/Adapter/Mysql.php +++ b/src/Adapter/Mysql.php @@ -131,9 +131,10 @@ public function beginTransaction(?int $flags = null, ?string $name = null): Mysq } $this->isTransaction = true; - $this->transactionDepth++; } + $this->transactionDepth++; + return $this; } @@ -172,20 +173,18 @@ public function commit(?int $flags = null, ?string $name = null): Mysql */ public function rollback(?int $flags = null, ?string $name = null): Mysql { - $this->transactionDepth--; + $this->transactionDepth = 0; - if ($this->transactionDepth == 0) { - if (($flags !== null) && ($name !== null)) { - $this->connection->rollback($flags, $name); - } else if ($flags !== null) { - $this->connection->rollback($flags); - } else { - $this->connection->rollback(); - } - - $this->isTransaction = false; + if (($flags !== null) && ($name !== null)) { + $this->connection->rollback($flags, $name); + } else if ($flags !== null) { + $this->connection->rollback($flags); + } else { + $this->connection->rollback(); } + $this->isTransaction = false; + return $this; } @@ -493,4 +492,4 @@ public function getTables(): array return $tables; } -} \ No newline at end of file +} diff --git a/src/Adapter/Pdo.php b/src/Adapter/Pdo.php index 0f176f4..bf74f1c 100644 --- a/src/Adapter/Pdo.php +++ b/src/Adapter/Pdo.php @@ -185,8 +185,10 @@ public function beginTransaction(): Pdo if ($this->transactionDepth == 0) { $this->connection->beginTransaction(); $this->isTransaction = true; - $this->transactionDepth++; } + + $this->transactionDepth++; + return $this; } @@ -214,12 +216,9 @@ public function commit(): Pdo */ public function rollback(): Pdo { - $this->transactionDepth--; - - if ($this->transactionDepth == 0) { - $this->connection->rollBack(); - $this->isTransaction = false; - } + $this->transactionDepth = 0; + $this->connection->rollBack(); + $this->isTransaction = false; return $this; } @@ -742,4 +741,4 @@ public function exec(mixed $sql): Pdo return $this; } -} \ No newline at end of file +} diff --git a/src/Adapter/Pgsql.php b/src/Adapter/Pgsql.php index 0f19740..968b83a 100644 --- a/src/Adapter/Pgsql.php +++ b/src/Adapter/Pgsql.php @@ -156,8 +156,8 @@ public function beginTransaction(): Pgsql if ($this->transactionDepth == 0) { $this->query('BEGIN TRANSACTION'); $this->isTransaction = true; - $this->transactionDepth++; } + $this->transactionDepth++; return $this; } @@ -169,12 +169,9 @@ public function beginTransaction(): Pgsql */ public function commit(): Pgsql { - $this->transactionDepth--; - - if ($this->transactionDepth == 0) { - $this->query('COMMIT'); - $this->isTransaction = false; - } + $this->transactionDepth = 0; + $this->query('COMMIT'); + $this->isTransaction = false; return $this; } @@ -186,12 +183,9 @@ public function commit(): Pgsql */ public function rollback(): Pgsql { - $this->transactionDepth--; - - if ($this->transactionDepth == 0) { - $this->query('ROLLBACK'); - $this->isTransaction = false; - } + $this->transactionDepth = 0; + $this->query('ROLLBACK'); + $this->isTransaction = false; return $this; } @@ -433,4 +427,4 @@ public function getTables(): array return $tables; } -} \ No newline at end of file +} diff --git a/src/Adapter/Sqlite.php b/src/Adapter/Sqlite.php index ae25fd9..9ad26c5 100644 --- a/src/Adapter/Sqlite.php +++ b/src/Adapter/Sqlite.php @@ -137,9 +137,10 @@ public function beginTransaction(): Sqlite if ($this->transactionDepth == 0) { $this->query('BEGIN TRANSACTION'); $this->isTransaction = true; - $this->transactionDepth++; } + $this->transactionDepth++; + return $this; } @@ -167,12 +168,9 @@ public function commit(): Sqlite */ public function rollback(): Sqlite { - $this->transactionDepth--; - - if ($this->transactionDepth == 0) { - $this->query('ROLLBACK'); - $this->isTransaction = false; - } + $this->transactionDepth = 0; + $this->query('ROLLBACK'); + $this->isTransaction = false; return $this; } @@ -476,4 +474,4 @@ public function getTables(): array return $tables; } -} \ No newline at end of file +} diff --git a/src/Adapter/Sqlsrv.php b/src/Adapter/Sqlsrv.php index 02c9f19..061d6fc 100644 --- a/src/Adapter/Sqlsrv.php +++ b/src/Adapter/Sqlsrv.php @@ -140,9 +140,10 @@ public function beginTransaction(): Sqlsrv if ($this->transactionDepth == 0) { sqlsrv_begin_transaction($this->connection); $this->isTransaction = true; - $this->transactionDepth++; } + $this->transactionDepth++; + return $this; } @@ -170,12 +171,9 @@ public function commit(): Sqlsrv */ public function rollback(): Sqlsrv { - $this->transactionDepth--; - - if ($this->transactionDepth == 0) { - sqlsrv_rollback($this->connection); - $this->isTransaction = false; - } + $this->transactionDepth = 0; + sqlsrv_rollback($this->connection); + $this->isTransaction = false; return $this; } @@ -481,4 +479,4 @@ public function getTables(): array return $tables; } -} \ No newline at end of file +} diff --git a/src/Record.php b/src/Record.php index b17452d..dbb2132 100644 --- a/src/Record.php +++ b/src/Record.php @@ -205,9 +205,7 @@ public static function start(): static|null $args = func_get_args(); $class = get_called_class(); if (Db::hasDb($class)) { - if (Db::db($class)->getTransactionDepth() == 0) { - Db::db($class)->beginTransaction(); - } + Db::db($class)->beginTransaction(); } if ($class !== 'Pop\Db\Record') { @@ -1064,4 +1062,4 @@ public static function __callStatic(string $name, array $arguments): Collection| return ($columns !== null) ? static::findBy($columns, $options, $asArray) : null; } -} \ No newline at end of file +} diff --git a/src/Record/AbstractRecord.php b/src/Record/AbstractRecord.php index 5d18383..b5a94dc 100644 --- a/src/Record/AbstractRecord.php +++ b/src/Record/AbstractRecord.php @@ -164,7 +164,7 @@ public function setPrimaryKeys(array $keys): AbstractRecord public function startTransaction(): AbstractRecord { $class = get_called_class(); - if ((Db::hasDb($class)) && (!Db::db($class)->isTransaction())) { + if (Db::hasDb($class)) { Db::db($class)->beginTransaction(); } $this->isTransaction = true; @@ -727,4 +727,4 @@ public function offsetUnset(mixed $offset): void $this->__unset($offset); } -} \ No newline at end of file +}