Website 🚀 contributte.org | Contact 👨🏻💻 f3l1x.io | Twitter 🐦 @contributte
This project is no longer being maintained. Please use contributte/database. |
---|
Composer | minetro/ntdb |
---|---|
Version | |
PHP | |
License |
Inspired by these articles:
- http://www.yiiframework.com/wiki/38/how-to-use-nested-db-transactions-mysql-5-postgresql/
- http://www.kennynet.co.uk/2008/12/02/php-pdo-nested-transactions/
- https://gist.github.com/neoascetic/5269127
Provide nested transaction via savepoints.
Support
- MySQL / MySQLi
- PostgreSQL
- SQLite
API
$t->begin
$t->commit
$t->rollback
$t->transaction
or$t->t
$t->promise
Starts transaction.
$t = new Transaction(new Connection(...));
$t->begin();
Commit changes in transaction.
$t = new Transaction(new Connection(...));
$t->begin();
// some changes..
$t->commit();
Revert changes in transaction.
$t = new Transaction(new Connection(...));
$t->begin();
try {
// some changes..
$t->commit();
} catch (Exception $e) {
$t->rollback();
}
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..
});
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)
}
);
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);
};
extensions:
ntdb: Minetro\Database\Transaction\DI\Transaction
That's all. You can let nette\di autowired it to your services/presenters.
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)
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..
});
}
}
This package was maintain by these authors.
Consider to support contributte development team. Also thank you for being used this package.