我试图在yii2中创建一个模块。我希望使用事件来处理模块中的某些事件,并使我的模块独立于我的核心。
现在我想要停止模块代码流,如果事件抛出甚至处理程序返回false。
看下面的代码:
function withdraw($amount, $description = null)
{
if (!(is_integer($amount) or is_float($amount) or is_double($amount))) {
throw new InvalidArgumentException("Amount should be in double.");
}
try {
$event = new TransactionEvent();
$event->setTransaction($transactionModel);
$event->setType($event::TYPE_WITHDRAW);
$this->trigger(\aminkt\userAccounting\UserAccounting::EVENT_WITHDRAW, $event);
$this->totalWithdraw += $amount;
if (!$this->save())
throw new RuntimeException("Purse cant update itself in withdraw action.");
$transaction->commit();
} catch (\Exception $e) {
throw $e;
} catch (\Throwable $e) {
throw $e;
}
}
这是我现在的代码。我想要下面的内容知道事件处理程序正常工作,我的事务正确保存到数据库中:
if($this->trigger(\aminkt\userAccounting\UserAccounting::EVENT_WITHDRAW, event)){
$this->totalWithdraw += $amount;
if (!$this->save())
throw new RuntimeException("Purse cant update itself in withdraw action.");
}else{
throw new RuntimeException("Transaction handler not worked correctly.");
}
谢谢。