Query Builder - Transaction Management

Efficiently manage your database transactions with QueryBuilder. Learn how to initiate, commit, and rollback transactions for precise control.

This page provides a practical guide with code samples for managing transactions using the QueryBuilder class in the PowerLitePdo library. The following methods are covered:

Loading the QueryBuilder Class

Before you can use the QueryBuilder class, you need to load it within the Dependency Injection (DI) container.

use Migliori\PowerLitePdo\Query\QueryBuilder;

$container = require_once __DIR__ . '/../src/bootstrap.php';
$queryBuilder = $container->get(QueryBuilder::class);

transactionBegin

This method starts a new database transaction.

Method Signature

public function transactionBegin(): bool

Examples

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

Db::transactionCommit

This method commits the current database transaction.

Method Signature

public function transactionCommit(): bool

Examples

$queryBuilder->transactionBegin();

// Perform some database operations...

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

Db::transactionRollback

This method rolls back the current database transaction.

Method Signature

public function transactionRollback(): bool

Examples

$queryBuilder->transactionBegin();

try {
    // Perform some database operations...

    $queryBuilder->transactionCommit(); // Commit the transaction
} catch (\Exception $e) {
    $queryBuilder->transactionRollback(); // Rollback the transaction in case of an error
}