ajkosh
(Ajaykaushiksharma087)
December 19, 2014, 3:15am
1
cebe i dont know i m doing right or not but before save is not working
below is code
controller code
public function actionCreate()
{
$model = new Employee();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
echo "false";
return $this->render('create', [
'model' => $model,
]);
}
}
model code
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$this->emp_no="EMP0001";
$this->institution_id=1;
$this->user_id=1;
return true;
} else {
return false;
}
}
mdmunir
(Misbahuldmunir)
December 19, 2014, 4:28am
2
What do you mean "is not working"?
beforeSave()
called after validation. Check your validation.
ajkosh
(Ajaykaushiksharma087)
December 19, 2014, 4:56am
3
it was working in 2.0.0 but when i upgrade to 2.0.1 it is not working and default implementation will trigger an EVENT_BEFORE_INSERT event when $insert is true,my rules are fines
ajkosh
(Ajaykaushiksharma087)
December 19, 2014, 5:48am
4
i got my answer but i did nt get this how it is done
i just changed controller code to below
public function actionCreate()
{
$model = new Employee();
if ($model->load(Yii::$app->request->post())) {
$model->save(false); // this line i have changed so i have to set false then so beforesave will trigger
return $this->redirect([‘view’, ‘id’ => $model->id]);
} else {
echo "false";
return $this->render(‘create’, [
‘model’ => $model,
]);
}