Method that won't redirect to the update page

In the controller we have an update method, how do make it when I click the update button it won’t redirect it on the update page it will just refresh the page and change the the $model->status = (‘Done’);




 public function actionUpdate($id)

    {

        $model = $this->findModel($id);


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

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

        } else {

            $model->status = ('Done');

            $model->time_end = date('y-m-d h-i-s');

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

                'model' => $model,

            ]);

        }

    }

Do you mean to say “Method won’t redirect to the view page”?

Your code for actionUpdate looks quite natural and I don’t see anything wrong with it. If it won’t redirect to the view page, it should be just because the “save” method fails everytime. What error do you get?

What I meant is that, how do I make it that it won’t redirect it to the update page, the page will just refresh and the model status will be changed into ‘Done’.

Sorry I have very bad english.

Edit: After reading the documentation I have come up with this:




 public function actionUpdate($id)

    {

        $model = $this->findModel($id);


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

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

        } else {

            $model->status = ('Done');

            $model->time_end = date('y-m-d h-i-s');

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

        

        }

    }



The page refreshes now but it doesn’t change the status into ‘Done’

Ah, OK. You don’t render status and time_end in the form of the view, do you?

Then the following will work as expected.




 public function actionUpdate($id)

    {

        $model = $this->findModel($id);

        $model->status = ('Done');

        $model->time_end = date('y-m-d h-i-s');


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

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

        } else {

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

                'model' => $model,

            ]);

        }

    }



I have tried your suggestions but when it stills redirect me to the update page, it supposed to be the view page will just refresh and the status and time_end will just change without going to the update page.

Nvm I have already figure it out




public function actionUpdate($id)

    {

        $model = $this->findModel($id);

        $model->status = ('Done');

        $model->time_end = date('y-m-d h-i-s');


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

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

        } else {

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

                'model' => $model,

            ]);

        }

    }



The following would be enough.




public function actionUpdate($id)

    {

        $model = $this->findModel($id);

        $model->status = ('Done');

        $model->time_end = date('y-m-d h-i-s');

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

    }



It works but why It does not save? It changes but when I go back to the index page it does not save.

Oh, I’m sorry. You have to save, of course.




public function actionUpdate($id)

    {

        $model = $this->findModel($id);

        $model->status = ('Done');

        $model->time_end = date('y-m-d h-i-s');

        $model->save();

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

    }