在yii2中的afterSave重定向

Den*_*G B 5 php yii2

我的模型中有一个afterSave函数,它根据用户给出的开始时间和持续时间保存某些服务的到期日期.我的afterSave工作正常,但在保存模型而不是显示空白页面后,它没有被重定向.

模型:

public function afterSave($insert)
{

    $month= "+".$this->duration_in_months." month";
    $this->exp_date=date("Y-m-d H:i:s",strtotime($month));
    $this->save(['exp_date']);

    return parent::afterSave($insert);
} 
Run Code Online (Sandbox Code Playgroud)

控制器:

if($model->save())

    return $this->redirect(['view', 'id' => $model->sub_id]);

} 
Run Code Online (Sandbox Code Playgroud)

我如何重定向afterSave?提前感谢!

Kun*_*the 3

正确的方法是从控制器调用save(),这将隐式调用afterSave()

您只需在控制器操作中执行此操作 -

if($model->save()) { $this->redirect(......); }