Skip to content

Latest commit

 

History

History

.docs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Contributte / Database

Content

Resources

Inspired by these articles:

Transaction

Provides nested transaction via savepoints.

Support

  • MySQL / MySQLi
  • PostgreSQL
  • SQLite

Usage

As with any other extension, you need to register it.

extensions:
	ntdb: Contributte\Database\DI\TransactionExtension

That's all. You can now let nette\di autowire it to your services/presenters.

NEON

Register it as a service in your config file.

services:
	- Contributte\Database\Transaction\Transaction

On multiple connections you have to specify which one to use.

services:
	- Contributte\Database\Transaction\Transaction(@nette.database.one.connection)
	# or
	- Contributte\Database\Transaction\Transaction(@nette.database.two.connection)

API

  • $t->begin
  • $t->commit
  • $t->rollback
  • $t->transaction or $t->t
  • $t->promise

Begin

Starts a transaction.

$t = new Transaction(new Connection(...));
$t->begin();

Commit

Commits changes in a transaction.

$t = new Transaction(new Connection(...));
$t->begin();
// some changes..
$t->commit();

Rollback

Reverts changes in a transaction.

$t = new Transaction(new Connection(...));

$t->begin();
try {
	// some changes..
	$t->commit();
} catch (Exception $e) {
	$t->rollback();
}

Transaction

Combines begin, commit and rollback to one method.

On success it commits changes, if an exception is thrown it rolls back changes.

$t = new Transaction(new Connection(...));

$t->transaction(function() {
	// some changes..
});

// or alias

$t->t(function() {
	// some changes..
});

Promise

Another approach to transactions.

$t = new Transaction(new Connection(...));

$t->promise()->then(
	function() {
		// Logic.. (save/update/remove some data)
	},
	function () {
		// Success.. (after commit)
	},
	function() {
		// Failed.. (after rollback)
	}
);

UnresolvedTransactionException

Logs unresolved transaction.

Idea by Ondrej Mirtes (https://ondrej.mirtes.cz/detekce-neuzavrenych-transakci).

$t = new Transaction(new Connection(...));
$t->onUnresolved[] = function($exception) {
Tracy\Debugger::log($exception);
};

Usage

use Contributte\Database\Transaction\Transaction;

class MyRepository {

	function __construct(Connection $connection) {
		$this->transaction = new Transaction($connection);
	}

	// OR

	function __construct(Context $context) {
		$this->transaction = new Transaction($context->getConnection());
	}
}

class MyPresenter {

	public function processSomething() {
		$transaction->transaction(function() {
			// Save one..

			// Make other..

			// Delete from this..

			// Update everything..
		});
	}
}