minetro / ntdb
0.9.1
2015-09-23 17:40 UTC
Requires
- php: >= 5.6
Requires (Dev)
- nette/database: ~2.3.0
- nette/tester: ~1.5.0
This package is not auto-updated.
Last update: 2022-02-01 12:50:10 UTC
README
网站 🚀 contributte.org | 联系 👨🏻💻 f3l1x.io | Twitter 🐦 @contributte
免责声明
⚠️ | 该项目不再维护。请使用 contributte/database。 |
---|
Composer | minetro/ntdb |
---|---|
版本 | |
PHP | |
许可证 |
资源
受以下文章启发
- https://yiiframework.cn/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
使用方法
通过保存点提供嵌套事务。
支持
- MySQL / MySQLi
- PostgreSQL
- SQLite
API
$t->begin
$t->commit
$t->rollback
$t->transaction
或$t->t
$t->promise
开始
开始事务。
$t = new Transaction(new Connection(...)); $t->begin();
提交
提交事务中的更改。
$t = new Transaction(new Connection(...)); $t->begin(); // some changes.. $t->commit();
回滚
回滚事务中的更改。
$t = new Transaction(new Connection(...)); $t->begin(); try { // some changes.. $t->commit(); } catch (Exception $e) { $t->rollback(); }
事务
将开始、提交和回滚合并为一个方法。
在成功时提交更改,如果抛出异常则回滚更改。
$t = new Transaction(new Connection(...)); $t->transaction(function() { // some changes.. }); // or alias $t->t(function() { // some changes.. });
承诺
对事务的另一种态度。
$t = new Transaction(new Connection(...)); $t->promise()->then( function() { // Logic.. (save/update/remove some data) }, function () { // Success.. (after commit) }, function() { // Failed.. (after rollback) } );
UnresolvedTransactionException
记录未解决的事务。
想法来自 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
就这么多。您可以允许 nette\di 自动将其注入到您的服务/演示者中。
NEON
在配置文件中将它注册为服务。
services: - Minetro\Database\Transaction\Transaction
在多个连接中您必须指定一个。
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.. }); } }
开发
此软件包由以下作者维护。
请考虑 支持 contributte 开发团队。同时感谢您使用此软件包。