Commit 4ff28fb9 by Alexander Makarov

Added callable typehint and active transaction check to Connection::transaction()

parent ceff7cc7
...@@ -459,19 +459,21 @@ class Connection extends Component ...@@ -459,19 +459,21 @@ class Connection extends Component
/** /**
* Executes callback provided in a transaction. * Executes callback provided in a transaction.
* *
* @param mixed $callback a valid PHP callback that performs the job. Accepts transaction instance as parameter. * @param callable $callback a valid PHP callback that performs the job. Accepts transaction instance as parameter.
* @param string|null $isolationLevel The isolation level to use for this transaction. * @param string|null $isolationLevel The isolation level to use for this transaction.
* See [[Transaction::begin()]] for details. * See [[Transaction::begin()]] for details.
* @throws \Exception * @throws \Exception
* @return mixed result of callback function * @return mixed result of callback function
*/ */
public function transaction($callback, $isolationLevel = null) public function transaction(callable $callback, $isolationLevel = null)
{ {
$transaction = $this->beginTransaction($isolationLevel); $transaction = $this->beginTransaction($isolationLevel);
try { try {
$result = call_user_func($callback, $transaction); $result = call_user_func($callback, $transaction);
$transaction->commit(); if ($transaction->isActive) {
$transaction->commit();
}
} catch (\Exception $e) { } catch (\Exception $e) {
$transaction->rollBack(); $transaction->rollBack();
throw $e; throw $e;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment