CakePHP 3.0 - 如何在不使用执行的事务上记录错误?
CakePHP 3.0 - How do I log an error on transactionals that don't use execute?
我正在尝试 运行 一个像这样的函数:
$records->connection()->transactional(function () use ($records, $entities) {
foreach ($entities as $record) {
$records->save($record, ['atomic' => false]);
}
});
有没有办法检查并查看此事务是否引发错误,或者 运行以这种方式执行事务与执行是否存在固有错误?
最终目标是更新一定数量的实体,但如果抛出错误则更新 none。上面的函数足够抽象,可以更改实体的不同方面,因此使用此方法而不是具体执行,以便于通过实体进行保存。
ConnectionInterface::transactional()
将发出回滚以防回调 returns false
或抛出错误(之后会重新抛出)。
引自文档:
[...]
The transactional method will do the following:
- Call
begin
.
- Call the provided closure.
- If the closure raises an exception, a rollback will be issued. The original exception will be re-thrown.
- If the closure returns
false
, a rollback will be issued.
- If the closure executes successfully, the transaction will be committed.
[...]
Cookbook > Dabase Access & ORM > Database Basis > Using Transactions
[...]
Returns mixed
The return value of the callback.
[...]
API > \Cake\Datasource\ConnectionInterface::transactional()
所以异常已经被覆盖,你可以简单地捕获它们,return 值是从你的闭包中 returned 的值,所以你可能还需要 return false
来自你的闭包以防 Table::save()
调用失败。
我正在尝试 运行 一个像这样的函数:
$records->connection()->transactional(function () use ($records, $entities) {
foreach ($entities as $record) {
$records->save($record, ['atomic' => false]);
}
});
有没有办法检查并查看此事务是否引发错误,或者 运行以这种方式执行事务与执行是否存在固有错误?
最终目标是更新一定数量的实体,但如果抛出错误则更新 none。上面的函数足够抽象,可以更改实体的不同方面,因此使用此方法而不是具体执行,以便于通过实体进行保存。
ConnectionInterface::transactional()
将发出回滚以防回调 returns false
或抛出错误(之后会重新抛出)。
引自文档:
[...]
The transactional method will do the following:
- Call
begin
.- Call the provided closure.
- If the closure raises an exception, a rollback will be issued. The original exception will be re-thrown.
- If the closure returns
false
, a rollback will be issued.- If the closure executes successfully, the transaction will be committed.
[...]
Cookbook > Dabase Access & ORM > Database Basis > Using Transactions
[...]
Returns
mixed
The return value of the callback.[...]
API > \Cake\Datasource\ConnectionInterface::transactional()
所以异常已经被覆盖,你可以简单地捕获它们,return 值是从你的闭包中 returned 的值,所以你可能还需要 return false
来自你的闭包以防 Table::save()
调用失败。