I’m working on a project with Yii. I am working on the backend, at the moment.
I’m implementing a function in a controller that should load a model, change a value in the database (a tinyint that changes from 0 to 1) for that particular model and redirect to another url.
I want to use this function in the same way as it’s used the “delete” function generated by the CRUD generator.
So inspiring myself on the “delete” function, I am trying this one for the “accept” function:
public function actionAccept($id) {
if(Yii::app()->request->isPostRequest)
{
$model = $this->loadModel($id);
$model->testimonial_accepted = '1';
$model->save();
// if AJAX request we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
I am calling the “accept” function in a view, like this:
array('label'=>'Accept Client', 'url'=>'#', 'linkOptions'=>array('submit'=>array('accept','id'=>$model->id),'confirm'=>'Are you sure you want to accept this item?')),
When run, there’s no error. It loads the right model but it doesn’t change the value in the database (testimonial_accepted). Do you have any idea why? What am I doing wrong?
Thanks in advance,
Supialios.
First, check if this works:
If it works, this mean that model’s data is not saved because validation fail (e.g. check model’s rules and eventually define new scenario).
And in this case I think it’s more appropriate to use update instead of save method: