event does not trigger and I dont know how to debug it

I am trying to solve "do B action when A happens". I try 2 approaches: 1 with event handler and 1 with after save. The event handler does not work as is the call back function does not run while the After Save works fine.




// this works

class Book extends \yii\db\ActiveRecord

{

    public function afterSave($insert, $changedAttributes) {

        parent::afterSave($insert, $changedAttributes);

        

        $miscAlertMailer = new \common\components\MiscAlertMailer();

        $miscAlertMailer->execute($this);


    }

}






//another way to do. it should do the same thing as the above , but it does not. The execute() never call because I put in a die() in

// it. So after update a book, the program should die, but it doesnt. 

class Book extends \yii\db\ActiveRecord

{

   public function init()

    {

        parent::init();

        


        $miscAlertMailer = new \common\components\MiscAlertMailer();

        

        $this->on(\yii\db\ActiveRecord::EVENT_AFTER_INSERT, [$miscAlertMailer, 'execute', $this]);

        $this->on(\yii\db\ActiveRecord::EVENT_AFTER_UPDATE, [$miscAlertMailer, 'execute', $this]);


        

    }

}



Appreciate any helps

I figure it out




class Book extends \yii\db\ActiveRecord

{

   public function init()

    {

        parent::init();

        


        $miscAlertMailer = new \common\components\MiscAlertMailer();

        

        $this->on(\yii\db\ActiveRecord::EVENT_AFTER_INSERT, [$miscAlertMailer, 'execute']);

        $this->on(\yii\db\ActiveRecord::EVENT_AFTER_UPDATE, [$miscAlertMailer, 'execute']);


        

    }

}



I passed too many parameter




//wrong

$this->on(\yii\db\ActiveRecord::EVENT_AFTER_INSERT, [$miscAlertMailer, 'execute', $this]);


//correct

$this->on(\yii\db\ActiveRecord::EVENT_AFTER_INSERT, [$miscAlertMailer, 'execute']);



Now the call back is called, but how do I access the instance of the book in the callback ?

That would be $event->sender.

Note the signature of an event handler.

Thanks,

I find out another way




//correct

$this->on(\yii\db\ActiveRecord::EVENT_AFTER_INSERT, [$miscAlertMailer, 'execute'], $extraData);



more details @ http://www.yiiframework.com/doc-2.0/yii-base-component.html#on()-detail