Answer the question
In order to leave comments, you need to log in
Why is the data not saved correctly in the Yii2 database?
Good day! There is a table with a field "from_form" tiny_int(1). Here is the action:
public function actionView($id)
{
$model = $this->findModel($id);
if ($model->from_form > 2) {
$new_model = Mails::findOne($id);
$new_model->from_form = $model->from_form - 2;
$new_model->save();
}
return $this->render('view', [
'model' => $model,
]);
}
$new_model->from_form = 2;
, 3 is still recorded. Tell me what am I doing wrong? If you need any more information, write in the comments, I will supplement. Thanks in advance!
Answer the question
In order to leave comments, you need to log in
Good afternoon.
Move these calculations to the model, to the beforeSave() method, and slightly change the line with the
PS calculation. It should look something like this:
public function beforeSave($insert)
{
if(parent::beforeSave($insert)){
if((self::getOldAttribute('from_form') > 2){
$this->from_form = ($this->from_form - 2)
}
return true;
}
return false;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question