Transactional PHP

Component ID

2730851

Component name

Transactional PHP

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Downloads

195

Component created

Component changed

Component body

Installation

  1. Apply the core patch bundled with this module.
  2. Update autoload with dependencies:

    %> composer drupal-update 
    

Configuration

None

Using the default database connection

<?php

$command = Drupal::service('transactionalphp');

$command->onCommit(function () {
  // This code will be run immediately, because no transaction is in progress.
});

$tx1 = db_transaction();

$command->onCommit(function () {
  // This code will be run when the outer transaction is committed.
});

$tx2 = db_transaction();

$command->onCommit(function () {
  // This code will not be run, because the inner transaction is committed.
});

$tx2->rollback();
unset($tx1);

$command->onCommit(function () {
  // This code will be run immediately, because no transaction is in progress.
});

?>

Using a specific database connection

<?php

$connection = Database::getConnection('my_target', 'my_key');
$command = Drupal::service('transactionalphp.factory')->get($connection);

$command->onCommit(function () {
  // This code will be run immediately, because no transaction is in progress.
});

$tx1 = $connection->startTransaction();

$command->onCommit(function () {
  // This code will be run when the outer transaction is committed.
});

$tx2 = $connection->startTransaction();

$command->onCommit(function () {
  // This code will not be run, because the inner transaction is committed.
});

$tx2->rollback();
unset($tx1);

$command->onCommit(function () {
  // This code will be run immediately, because no transaction is in progress.
});

?>