I have an afterSave function in my model which saves the expiry date of some service based on the start period and duration given by the user. My afterSave
works fine,but it is not getting redirected after saving the model instead showing a blank page.
Model:
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]);
}
How can i redirect afterSave?Thanks in advance!
Proper way is to call the save()
from the Controller, which will call the afterSave()
implicitly.
You only have to do this in the Controller-Action -
if($model->save()) {
$this->redirect(......);
}
Your controller is OK, but I see something odd in your "afterSave" method. There is
$this->save(['exp_date'])
First of all standard ActiveRecord "save" must have boolean as its first argument. Next is you will get recursion here - as "afterSave" method is being called inside "save" method.
So I suppose the real problem is that you don't have any error displayed. Try to enable error reporting in your index.php before including Yii:
error_reporting(E_ALL);
ini_set('display_errors', '1');
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
This is only for development.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With