Db - Transaction Management

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:

Loading the Db Class

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']);
};

transactionBegin

This method starts a new database transaction.

Method Signature

public function transactionBegin(): bool

Examples

$db->transactionBegin();
// Perform some database operations...

Db::transactionCommit

This method commits the current database transaction.

Method Signature

public function transactionCommit(): bool

Examples

$db->transactionBegin();

// Perform some database operations...

$db->transactionCommit(); // Commit the transaction

Db::transactionRollback

This method rolls back the current database transaction.

Method Signature

public function transactionRollback(): bool

Examples

$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
}