diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index ab56d662..f0961045 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -1221,7 +1221,7 @@ public function exportClasses(array $classes) } } - $connection->commit(); + Doctrine_TransactionHelper::commitIfInTransaction($connection); } } diff --git a/lib/Doctrine/Migration.php b/lib/Doctrine/Migration.php index 0208ce54..8a6fa1d7 100644 --- a/lib/Doctrine/Migration.php +++ b/lib/Doctrine/Migration.php @@ -335,24 +335,21 @@ public function migrate($to = null, $dryRun = false) if ($dryRun) { return false; - } else { - $this->_throwErrorsException(); - } - } else { - if ($dryRun) { - $this->_connection->rollback(); - if ($this->hasErrors()) { - return false; - } else { - return $to; - } - } else { - $this->_connection->commit(); - $this->setCurrentVersion($to); - return $to; } + + $this->_throwErrorsException(); + } + + if ($dryRun) { + $this->_connection->rollback(); + + return !$this->hasErrors(); } - return false; + + Doctrine_TransactionHelper::commitIfInTransaction($this->_connection); + $this->setCurrentVersion($to); + + return true; } /** @@ -437,8 +434,9 @@ public function getMigrationClass($num) /** * Throw an exception with all the errors trigged during the migration * - * @return void - * @throws Doctrine_Migration_Exception $e + * @throws Doctrine_Migration_Exception + * + * @return never */ protected function _throwErrorsException() { @@ -559,4 +557,4 @@ protected function _createMigrationTable() return false; } } -} \ No newline at end of file +} diff --git a/lib/Doctrine/TransactionHelper.php b/lib/Doctrine/TransactionHelper.php new file mode 100644 index 00000000..c594a91f --- /dev/null +++ b/lib/Doctrine/TransactionHelper.php @@ -0,0 +1,24 @@ + + */ +final class Doctrine_TransactionHelper +{ + /** + * Execute a commit on the given connection, only if a transaction already started. + */ + public static function commitIfInTransaction(Doctrine_Connection $connection): void + { + $handler = $connection->getDbh(); + + // Attempt to commit while no transaction is running results in exception since PHP 8 + pdo_mysql combination + if ($handler instanceof PDO && !$handler->inTransaction()) { + return; + } + + $connection->commit(); + } +}