Contributte Database
# Contributte / Database
# Content
# Resources
Inspired by these articles:
- http://www.yiiframework.com/wiki/38/how-to-use-nested-db-transactions-mysql-5-postgresql/ (opens new window)
- http://www.kennynet.co.uk/2008/12/02/php-pdo-nested-transactions/ (opens new window)
- https://gist.github.com/neoascetic/5269127 (opens new window)
# 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
1
2
2
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
1
2
2
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)
1
2
3
4
2
3
4
# API
- $t->begin
- $t->commit
- $t->rollback
- $t->transactionor- $t->t
- $t->promise
# Begin
Starts a transaction.
$t = new Transaction(new Connection(...));
$t->begin();
1
2
2
# Commit
Commits changes in a transaction.
$t = new Transaction(new Connection(...));
$t->begin();
// some changes..
$t->commit();
1
2
3
4
2
3
4
# Rollback
Reverts changes in a 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
2
3
4
5
6
7
8
9
# 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..
});
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 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)
	}
);
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# UnresolvedTransactionException
Logs 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
2
3
4
# 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..
		});
	}
}
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
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