How to use the afterSave event in Yii2?

How do I use the afterSave event?

I write in the model, but it is not called when saving data to 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();
    }



}
Author: gilo1212, 2016-07-04

1 answers

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 do everything we need.

 2
Author: Urmuz Tagizade, 2016-07-04 08:53:07