php - yii2 中的 afterSave 重定向

标签 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);
} 

Controller :

if($model->save())

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

} 

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

最佳答案

正确的方法是从 Controller 调用save(),它会隐式调用afterSave()

您只需在 Controller-Action 中执行此操作 -

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

关于php - yii2 中的 afterSave 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23424827/

相关文章:

yii2 - 跟踪 Yii2 中每个请求的数据

php - Laravel 定义关系

PHP pdo 带日期的复杂查询

php - 正则表达式删除带数字的单词

php - Yii2 将默认路由更改为另一个 namespace

php - 使用 PUT 上传 yii2 REST Api 文件

php - 如何在 Yii2 的控制台 Controller 中使用模型?

php - 如何根据星级过滤mysql数据库中的用户

php - mysql的多个实例正在运行-centos7

module - Yii2 模块模型