For example, I create a new model and save it
$model = new Comment();
$model->scenario = 'guest';
$model->save();
In model i have afterSave callback:
public function afterSave($insert, $changedAttributes)
{
if (($this->scenario == 'user' || $this->scenario == 'guest') && $this->isNewRecord)
\Yii::$app->mailer->compose('new_comment', ['model' => $this])
->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
->setTo(Email::getByGroup(Email::COMMENTS))
->setSubject('Some subject')
->send();
parent::afterSave($insert, $changedAttributes);
}
Because, the isNewRecord property is overwritten I can't detect was the model saved right now or not.
Maybe it will be better don't overwrite isNewRecord property after first calling of the save() method?
Use $insert value.
Most helpful comment
Use $insert value.