MeekroDB Quick Start Docs ORM Docs FAQ
Download GitHub

Transactions

DB::startTransaction(), DB::commit(), and DB::rollback()

Normally, these three commands are just shortcuts for START TRANSACTION, COMMIT, and ROLLBACK.

// only keep the password change if >3 people were affected
DB::startTransaction();
DB::query("UPDATE accounts SET password=%s WHERE password=%s", 'sdfwsert4rt', 'hello');
$counter = DB::affectedRows();
if ($counter > 3) {
  echo "$counter people just got their password changed!!\n";
  DB::commit();
} else {
  echo "Too few people got their password changed!\n";
  DB::rollback();
}

When you enable DB::$nested_transactions, the transaction commands take on a special meaning. You can now use DB::startTransaction() within a transaction to "nest" transactions. This is accomplished internally with SAVEPOINTs.

DB::$nested_transactions = true;
DB::startTransaction();
// .. some queries..
$depth = DB::startTransaction(); // start nested transaction
echo "We are currently $depth transactions deep"; // 2

// .. some queries..
DB::commit(); // commit inner transaction
// .. some queries..
DB::commit(); // commit outer transaction