From 98be0312f8ff89791095c1396f9d3e62c1812bbf Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 29 Jun 2022 14:28:46 +1200 Subject: [PATCH] FIX PDO in PHP 8 --- src/ORM/Connect/PDOConnector.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/ORM/Connect/PDOConnector.php b/src/ORM/Connect/PDOConnector.php index 4a5c81f018e..9d881d6a21c 100644 --- a/src/ORM/Connect/PDOConnector.php +++ b/src/ORM/Connect/PDOConnector.php @@ -7,6 +7,7 @@ use PDO; use PDOStatement; use InvalidArgumentException; +use PDOException; /** * PDO driver database connector @@ -109,10 +110,15 @@ public function getOrPrepareStatement($sql) } // Generate new statement - $statement = $this->pdoConnection->prepare( - $sql, - [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY] - ); + try { + $statement = $this->pdoConnection->prepare( + $sql, + [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY] + ); + } catch (PDOException $e) { + // Prior to PHP 8, this failed silently, so setting false here is backwards compatible + $statement = false; + } // Wrap in a PDOStatementHandle, to cache column metadata $statementHandle = ($statement === false) ? false : new PDOStatementHandle($statement); @@ -558,7 +564,15 @@ public function transactionRollback($savepoint = null) } $this->inTransaction = false; - return $this->pdoConnection->rollBack(); + try { + return $this->pdoConnection->rollBack(); + } catch (PDOException $e) { + // A PDOException will be thrown if there is no active transaction in PHP 8+ + // Prior to PHP 8, this failed silently, so returning false here is backwards compatible + // Note: $this->inTransaction may not match the 'in-transaction' state in PDO + // https://www.php.net/manual/en/pdo.rollback.php + return false; + } } public function transactionDepth()