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

# Disclaimer

⚠️ This project is no longer being maintained. Please use contributte/database (opens new window).
Composer minetro/ntdb (opens new window)
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();
1
2

# Commit

Commit changes in transaction.

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

# Rollback

Revert changes in transaction.

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

$t->begin();
try {
	// some changes..
	$t->commit();
} catch (Exception $e) {
	$t->rollback();
}
1
2
3
4
5
6
7
8
9

# 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..
});
1
2
3
4
5
6
7
8
9
10
11

# 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)
	}
);
1
2
3
4
5
6
7
8
9
10
11
12
13

# UnresolvedTransactionException

Log unresolved transaction.

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

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

# Nette

# EXTENSION

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

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
1
2

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)
1
2
3
4

# 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..
		});
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# Development

This package was maintain by these authors.


Consider to support (opens new window) contributte development team. Also thank you for being used this package.