Post parameters

Hi,

I’m having problems accessing parameters sent in post.




$model = new Commandes();


        if ($model->load(Yii::$app->request->post()) ) {


            $model_n = Commandes::find()->where(['num_de_serie' => $model->num_de_serie ])->one();


            $model_n->nv_agence_id      = $model->nv_agence_id;

            $model_n->date_reafectation = $model->date_reafectation;

            $model_n->nv_num_pwc        = $model->nv_num_pwc;


            if($model_n->save())

                return $this->redirect(['view', 'id' => $model_n->num_de_serie]);

        } else {

            return $this->render('reaffectation', ['model' => $model,]);

          }



I added those three attribute after generating the model with GII and expect ‘$model->num_de_serie’ (this was created originally) they’re all NULL even even though the debugger show this :

$_POST

Name Value

_csrf ‘QWlURS42Y3hzAwIWaW81HQIiZQFEdxFKNF1hAHl3UyAkAC51dE8pKQ==’

Commandes [

'num_de_serie' => 'koko'


'nv_agence_id' => 'XXQ06J'


'date_reafectation' => '2016-04-30'


'nv_num_pwc' => 'iioioo'

]

Any suggestion ?

Have you got num_de_serie attribute in Commandes model rules() method?

Yes (required).

Why ? it get to the controller without any problem it’s the other attributes that I can’t get.

At which point you test the value of $model->num_de_serie and what you expect to see?

What other attributes you can’t get?

So after reading your first answer I decided to add some rules for those attribute and it worked !! I guess rules are mandatory for every attribute ?? I didn’t know that.

Thanks a lot :) .

Yup, see the load() method.

If you don’t have a rule for the attributes, they are considered “unsafe” to copy across from the post variables. If there is no rule required but you still need them to copy across, you can use the “safe” rule.