minetro/ntdb

此包已被弃用且不再维护。作者建议使用 contributte/database 包。

Nette 数据库嵌套事务

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

网站 🚀 contributte.org | 联系 👨🏻‍💻 f3l1x.io | Twitter 🐦 @contributte

免责声明

⚠️ 该项目不再维护。请使用 contributte/database
Composer minetro/ntdb
版本
PHP
许可证

资源

受以下文章启发

使用方法

通过保存点提供嵌套事务。

支持

  • 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..
		});
	}
}

开发

此软件包由以下作者维护。

538058?v=3&s=80

请考虑 支持 contributte 开发团队。同时感谢您使用此软件包。