Master Transaction Management with PowerLite PDO documentation. Understand how to commit, rollback, and manage your database transactions efficiently.
This page provides a practical guide with code samples for managing transactions using the Db
class in the PowerLitePdo library. The following methods are covered:
To use the Db
class, you first need to load it within the Dependency Injection (DI) container. Here's how you can do it:
$container['db'] = function ($container) {
return new \Migliori\PowerLitePdo\Db($container['settings']['db']);
};
This method starts a new database transaction.
public function transactionBegin(): bool
$db->transactionBegin();
// Perform some database operations...
This method commits the current database transaction.
public function transactionCommit(): bool
$db->transactionBegin();
// Perform some database operations...
$db->transactionCommit(); // Commit the transaction
This method rolls back the current database transaction.
public function transactionRollback(): bool
$db->transactionBegin();
try {
// Perform some database operations...
$db->transactionCommit(); // Commit the transaction
} catch (\Exception $e) {
$db->transactionRollback(); // Rollback the transaction in case of an error
}