How to use the afterSave event?

I write in the model, but it is not called when saving data in the database.

public function afterSave($insert, $changedAttributes) { var_dump("ooooooooo"); } 

Here is my model:

 <?php namespace app\modules\userpanel\models\userpanel; use app\models\db_table\Tablename; use yii\base\Model; use Yii; use yii\base\Event; use yii\db\ActiveRecord; class form extends Model { public $user_id; public function rules() { return [ ['user_id', 'required'], ]; } public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); var_dump("ooooooooo"); } public function addZakazDB() { $model2 = new Tablename(); $model2->user_id = $this->user_id; return $model2->save(); } } 

    1 answer 1

    You missed the parent method:

     public function afterSave($insert, $changedAttributes){ parent::afterSave($insert, $changedAttributes); //ниже ваш код var_dump("ooooooooo"); } 

    Why? Because we pass all the same parameters to the parent method and run it, and then we do everything we need.

    • Added but not working. Similarly, the same added to the model. - gilo1212
    • or parent::afterSave($insert, $changedAttributes); need to call from the method in which you add data to the database? - gilo1212
    • What does "parent method all the same parameters" mean? - gilo1212 '
    • this code should work, it means it doesn't work for you save - gudfar
    • one
      As already said gudfar - you probably will not get to save it. About the parameters. Meaning $ insert, $ changedAttributes - Urmuz Tagizade