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