O
O
OlegBas19962018-08-23 12:13:53
Yii
OlegBas1996, 2018-08-23 12:13:53

Error loading data from $_POST array to model?

Hello! After loading data from $_POST array Loading data via $model->load(Yii::$app->request->post()) there was a problem. After loading the data, the id_sec and is_show attributes are filled with the old data, although Yii::$app->request->post() contains new data, you have to write such a crutch
$model->id_sec = Yii::$app->request-> post("News")["id_sec"];
$model->is_show = Yii::$app->request->post("News")["is_show"];
Tell me how to solve the problem without using a crutch?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arman, 2018-08-23
@Arik

I suspect that you do not have these attributes in the rules, you need to at least specify safe

D
Dmitry, 2018-08-23
@slo_nik

Add a check for the id_sec, is_show properties to the rules rules and delete these two lines

$model->id_sec = Yii::$app->request->post("News")["id_sec"];
 $model->is_show = Yii::$app->request->post("News")["is_show"];

I suspect that the values ​​​​should be integers, so rules should have such a rule, You can combine with this rule Also read the documentation. ps Next, what kind of nonsense is this?
$model->created_at = time();
 $model->count_show = 0;
 $model->count_clicks = 0;
 $model->is_news = 1;

There is a TimestampBehavior for the first row to insert the time the post was created or edited. You need to add the following to the model:
public function behaviors()
    {
        return [
            TimestampBehavior::className()
        ];
    }

and remove from the action the line $model->created_at = time();
For the remaining three lines. Are you trying to set default values ​​or are you assigning new values?
If the values ​​are default, then this is also done in the rules:
['cout_show', 'count_click'], 'default', 'value' => 0]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question