0

I trigger an event in Yii2 transaction, and I want to know if the event handler succeed to commit the transaction, or fail to rollback.

Is a global variable or class const the right way?

What I do now is throwing an error in the event handlers.

feistiny
  • 95
  • 6
  • ummm... if your transaction is used within the `try{}catch(){}` block if there is an exception in the event it would be caught automatically isnt it? or you are talking about the correct execution of the event logic? – Muhammad Omer Aslam Jun 20 '19 at 11:55
  • Yes, I tend to mean the correct logic execution. – feistiny Jun 21 '19 at 01:26

1 Answers1

1

Usually you're using event object to store state of event. Create custom event:

class MyEvent extends Event {

    public $isCommited = false;
}

Use it on trigger and check the result:

$event = new MyEvent();
$this->trigger('myEvent', $event);
if ($event->isCommited) {
    // do something
}

In event handler you need to set this property:

function ($event) {
    // do something
    $event->isCommited = true;
}

If you want to break event flow you may use $handled property instead of isCommited and custom event.

rob006
  • 21,383
  • 5
  • 53
  • 74
  • `isCommitted` is more flexible, event handler E1 set it to false to rollback the event sender's transaction, but the event handler E2 can also exec the sqls inside E2 wrapped within a nested transaction. – feistiny Jun 21 '19 at 01:56