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:
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);
This method starts a new database transaction.
public function transactionBegin(): bool
$queryBuilder->transactionBegin();
// Perform some database operations...
This method commits the current database transaction.
public function transactionCommit(): bool
$queryBuilder->transactionBegin();
// Perform some database operations...
$queryBuilder->transactionCommit(); // Commit the transaction
This method rolls back the current database transaction.
public function transactionRollback(): bool
$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
}