Skip to content

Commit

Permalink
FIX PDO in PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jun 29, 2022
1 parent 01c27e6 commit 98be031
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/ORM/Connect/PDOConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PDO;
use PDOStatement;
use InvalidArgumentException;
use PDOException;

/**
* PDO driver database connector
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 98be031

Please sign in to comment.