H
H
hollanditkzn2017-06-26 18:49:54
Yii
hollanditkzn, 2017-06-26 18:49:54

How to understand why I have only one column not saved in the database?

I got such an error that when I want to change only the status, it is in the requests post, but in the end it saves everything except this column. That is, there the data remains old after saving. And at the same time, it does not give any errors
How I implemented one of the actons in the controller

public function actionAccept($id)
    {
        $model = $this->findModel($id);
//        $model->scenario = Zakaz::SCENARIO_DEFAULT;

        if ($model->load(Yii::$app->request->post())){
            $model->validate();
            if ($model->save()) {
//                d(Yii::$app->request->post());
                return $this->redirect(['admin', 'id' => $id]);
            } else {
                print_r($model->getErrors());
            }
        } else {
            return $this->renderAjax('accept', ['model' => $model]);
        }
    }

And in the model
const STATUS_DISAIN = 3;
const STATUS_MASTER = 6;
const STATUS_AUTSORS = 8;
const SCENARIO_DECLINED = 'declined';
    const SCENARIO_DEFAULT  = 'default';
public function scenarios()
    {
        return [
            self::SCENARIO_DECLINED => ['declined', 'required'],
            self::SCENARIO_DEFAULT => ['srok', 'number', 'description', 'phone', 'sotrud_name', 'required'],
        ];
    }
 public function rules()
    {
        return [
            [['srok', 'number', 'description', 'phone', 'sotrud_name'], 'required', 'on' => self::SCENARIO_DEFAULT],
            ['declined', 'required', 'message' => 'Введите причину отказа', 'on'=> self::SCENARIO_DECLINED],
            [['id_zakaz', 'id_tovar', 'oplata', 'fact_oplata', 'minut', 'time', 'number', 'status', 'action', 'id_sotrud', 'phone', 'id_shipping' ,'prioritet', 'statusDisain'], 'integer'],
            ...
        ];
    }

And here is what I see in post
Zakaz => array (1)
status => string (1) "3"

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Fedorov, 2017-06-27
@hollanditkzn

scenarios method returns a list of safe attributes, i.e. attributes that can be massively assigned a value, including using the load method you use. In your case, the status field is not specified as a safe attribute for any of the scenarios, so its value is not loaded into the model from the post. Accordingly, you need to add it to the list of safe attributes for the desired scenario. You can read more about safe attributes here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question