Answer the question
In order to leave comments, you need to log in
How in Yii2 in behavior can't get into onBeforeSave?
Good afternoon,
There is a behavior that I connect in the model:
...
public function behaviors()
{
return [
[
'class'=>LogBehavior::classname()
]
];
}
...
public function beforeSave($insert)
{
echo 'Сюда тоже не поапдаю(';
die;
if($insert) {
$this->user_id = Yii::$app->user->id;
}
return parent::beforeSave($insert); // TODO: Change the autogenerated stub
}
...
public function attach($owner)
{
parent::attach($owner);
$owner->on(ActiveRecord::EVENT_BEFORE_INSERT,[$this,'onBeforeSave']);
$owner->on(ActiveRecord::EVENT_BEFORE_UPDATE,[$this,'onBeforeSave']);
$owner->on(ActiveRecord::EVENT_AFTER_INSERT,[$this,'onAfterSave']);
$owner->on(ActiveRecord::EVENT_AFTER_UPDATE,[$this,'onAfterSave']);
$owner->on(ActiveRecord::EVENT_AFTER_DELETE,[$this,'onAfterDelete']);
...
}
public function onBeforeSave()
{
echo 'Сюда не попадаю, При сохранении модели эта запись не отображается';
die;
}
...
$model = new Results();
$model->option_id = $this->option_id;
$time = time();
$model->date_create = date('Y-m-d H:i:s',$time);
$model->date_update = date('Y-m-d H:i:s',$time);
$model->date_update_stamp = $time;
$model->date_create_stamp = $time;
$model->author_id = Yii::$app->user->id;
$model->exist = 1;
$model->sort = 1;
if(!$model->save()) {
echo $model->printErrors();
} else {
echo 'ok';
}
die();
Answer the question
In order to leave comments, you need to log in
It is not worth attaching to events in attach, it is better to do this:
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_INSERT => 'onBeforeSave',
ActiveRecord::EVENT_BEFORE_UPDATE => 'onBeforeSave',
];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question