Changin property of my model (ActiveRecord)

I have this form in "user/index":


<?php $form = ActiveForm::begin(['id' => 'form-profile-edit']); ?>

..HTML..

<?= Html::activeTextInput($model, 'email', ['placeholder' => Yii::t("app", "Page_Profile_Email"), 'class' => 'form-control']); ?>

..HTML..

<?php ActiveForm::end(); ?>

Here is my actionIndex on UserController. I am getting forwader with 302 response and id param in my URL (This actions make these in case of success) but dont change my email. If I print $model->email I see the unchanged email, but I see too in my post params that new email was sent ok. Can anybody help me?


    public function actionIndex()

    {

        $model = Yii::$app->user->identity;


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

            return $this->redirect(['index', 'id' => $model->id]);

        } else {

            return $this->render('index', [

                'model' => $model,

            ]);

        }

    }

My http info:


Request URL:http://myURL/backend/user/index?id=1 (HTTP 302)


_backendCsrf:OEZBZTh1OEV9PgQGZx1raH4TCh0BTG0hXxQkPU8/XHd3dDItaUVpBw==

User[email]:newemail@gmail.com

Sorry my english.

Your current actionIndex doesn’t get the ‘id’ parameter, because it doesn’t need it since it handles only the currently logged in user.

It should be either




public function actionIndex($id)

{

    $model = User::findOne($id);

    ...



or




        return $this->redirect(['index']);



Or, if you wanted to redirect to some other controller




        return $this->redirect(['other_controller/index', 'id' => $model->id]);



yes, its a profile form, where you can change the data from the loged user. I tried with this too but date dont change, anyway, its so weird that this redirect me with id in the url params (Enter in "save" condition) but dont change the data:


    public function actionIndex()

    {

        //$model = Yii::$app->user->identity;

        $model = User::findOne(['id' => Yii::$app->user->identity->id]);


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

            return $this->redirect(['index', 'id' => $model->id]);

        } else {

            return $this->render('index', [

                'model' => $model,

            ]);

        }

    }

I see in log this error:


Failed to set unsafe attribute 'email' in 'common\models\User'.

I tried with thisone too:


    public function actionIndex()

    {

        //$model = Yii::$app->user->identity;

        $model = User::findOne(['id' => Yii::$app->user->identity->id]);


        if ($model->load(Yii::$app->request->post()) && $model->update(true, ["email"])) {

            return $this->redirect(['index']);

        } else {

            return $this->render('index', [

                'model' => $model,

            ]);

        }

    }



This is correct if I only wants to update the email propertie of my Model? There are any restriction on update actions? I am reading about safe/unsafe properties but this is the first time that I have a problem changing a user propertie.

Again editing:

I added some rules to my UserModel and now I can edit my user fine. Whats the problem Here? The rules are this:


['email', 'filter', 'filter' => 'trim'],

            ['email', 'required', 'message' => Yii::t("app", "Validation_Email_Required")],

            ['email', 'email', 'message' => Yii::t("app", "Validation_Email_Format")],

            ['email', 'string', 'min' => 6, 'max' => 255, 'message' => Yii::t("app", "Validation_Email_Length"),

                'tooLong' => Yii::t("app", "Validation_Email_Length_Long"), 'tooShort' => Yii::t("app", "Validation_Email_Length_Short")],

            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t("app", "Validation_Email_Exists")],

I dont see any "safe", "unsafe" or special rules here.

Hmm, strange.

How did you check the value of the model, before and after the saving?

Did you do something like this?




    public function actionIndex()

    {

        $model = Yii::$app->user->identity;

        echo "initial state";

        echo $model->email;


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

            echo "loaded from post";

            echo $model->email;

            if ($model->save()) {

                echo "saved";

                echo $model->email;

                return $this->redirect(['index']);

            } else {

                echo "failed to save";

            }

        } else {

            echo "not loaded from post";

        }


        return $this->render('index', [

            'model' => $model,

        ]);

    }



Or, step-by-step inspection using some IDE with xdebug?