Andrew Hayes
Andrew Hayes

Reputation: 3

Creating a Revision History in Yii

I am using Yii. I currently have it set up so I can overwrite data which belongs to bug_id. But I would like to save this data to another model(History) under history_id and once you edit the bug it does not change the data saved in the history table.

The code I have for updating the bug data is:

public function actionUpdateBug($id)
{
    $model=$this->loadModel($id);
    $model->created_date= date('Y-m-d G:i:s');

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Bugs']))
    {
        $model->attributes=$_POST['Bugs'];
        if($model->save())
            $this->redirect(array('id'=>$model->bug_id));
    }

    $this->render('update',array(
        'model'=>$model,
    ));
} 

The Tables are as followed:

Bugs

bug_id - Primary Key,
bug_title,
img,
description,
project_id - Foreign Key,
created_date

History

history_id - Primary Key,
bug_id - Foreign Key,
bug_title,
img,
description,
created_date

Any help would be appreciated, Thank you

Upvotes: 0

Views: 175

Answers (1)

Yatin Mistry
Yatin Mistry

Reputation: 1264

Try this,

  // Save to History table
    $history = new History; 
    $history->attributes=$model->attributes;
    $history->save();

Your update function,

public function actionUpdateBug($id)
{
    $model=$this->loadModel($id);
    $model->created_date= date('Y-m-d G:i:s');

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Bugs']))
    {
        $model->attributes=$_POST['Bugs'];
        if($model->save()){

             // Save to History table
             $history = new History; 
             $history->attributes=$model->attributes;
             $history->save();

            $this->redirect(array('id'=>$model->bug_id));
        }
    }

    $this->render('update',array(
        'model'=>$model,
    ));
}

Upvotes: 1

Related Questions