carn4ge
(Pleasecall911kenneth)
December 9, 2017, 1:08pm
1
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,
]);
}
}
softark
(Softark)
December 9, 2017, 1:26pm
2
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?
carn4ge
(Pleasecall911kenneth)
December 9, 2017, 1:33pm
3
softark:
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’
softark
(Softark)
December 9, 2017, 2:08pm
4
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,
]);
}
}
carn4ge
(Pleasecall911kenneth)
December 9, 2017, 2:35pm
5
softark:
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,
]);
}
}
softark
(Softark)
December 9, 2017, 3:15pm
6
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]);
}
carn4ge
(Pleasecall911kenneth)
December 9, 2017, 3:49pm
7
softark:
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.
softark
(Softark)
December 9, 2017, 10:36pm
8
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]);
}