minetro/ntdb

This package is abandoned and no longer maintained. The author suggests using the contributte/database package instead.

Nette Database Nested Transaction

0.9.1 2015-09-23 17:40 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:50:10 UTC


README

68747470733a2f2f62616467656e2e6e65742f62616467652f737570706f72742f6769747465722f6379616e 68747470733a2f2f62616467656e2e6e65742f62616467652f737570706f72742f666f72756d2f79656c6c6f77 68747470733a2f2f62616467656e2e6e65742f62616467652f73706f6e736f722f646f6e6174696f6e732f463936383534

Website 🚀 contributte.org | Contact 👨🏻‍💻 f3l1x.io | Twitter 🐦 @contributte

Disclaimer

⚠️ This project is no longer being maintained. Please use contributte/database.
Composer minetro/ntdb
Version
PHP
License

Resources

Inspired by these articles:

Usage

Provide nested transaction via savepoints.

Support

  • MySQL / MySQLi
  • PostgreSQL
  • SQLite

API

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

Begin

Starts transaction.

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

Commit

Commit changes in transaction.

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

Rollback

Revert changes in transaction.

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

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

Transaction

Combine begin, commit and rollback to one method.

On success it commits changes, if exceptions is thrown it rollbacks changes.

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

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

// or alias

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

Promise

Another attitude to transaction.

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

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

UnresolvedTransactionException

Log 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);
};

Nette

EXTENSION

extensions:
	ntdb: Minetro\Database\Transaction\DI\Transaction

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

NEON

Register as service in your config file.

services:
	- Minetro\Database\Transaction\Transaction

On multiple connections you have to specific one.

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

Repository | Presenter

use Minetro\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..
		});
	}
}

Development

This package was maintain by these authors.

538058?v=3&s=80

Consider to support contributte development team. Also thank you for being used this package.