Prabhakaran
Prabhakaran

Reputation: 4013

yii to update record with loadModel

I am trying to update the whole form so trying to use massive assignment using attributes in yii. But getting error as

AcademicsController and its behaviors do not have a method or closure named "loadModel".

Here is my code used

Here $_GET['id'] = 5 and $_POST['courses'] is array

if(isset($_GET['id']) && isset($_POST['courses'])) {
    $course_id = $_GET['id'];
    $post = $_POST['courses'];
    $model = $this->loadModel($course_id);
        $model->save();
}

How to update all form fields?

Upvotes: 0

Views: 4916

Answers (1)

chameera
chameera

Reputation: 389

Add the method "loadModel" to AcademicsController.

 public function loadModel($id) {
    $model = Courses::model()->findByPk($id);
    if ($model === null)
        throw new CHttpException(404, 'The requested page does not exist.');
    return $model;
}

Adding above method to your controller will fix your issue. But inorder to update the model you need to assign POST data to model attributes. (I assume this is a single model)

//load model
$model = $this->loadModel($course_id);
//assign form values to model
$model->attributes = $_POST['courses'];
//save the model
$model->save();

Upvotes: 2

Related Questions