在Yii中创建修订历史记录

时间:2015-05-12 14:53:10

标签: php yii

我正在使用Yii。我目前已经设置好了,所以我可以覆盖属于bug_id的数据。但是我想将这些数据保存到history_id下的另一个模型(History)中,一旦编辑了bug,它就不会更改历史表中保存的数据。

我更新错误数据的代码是:

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,
    ));
} 

表格如下:

错误

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

历史

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

任何帮助将不胜感激,谢谢

1 个答案:

答案 0 :(得分:1)

试试这个,

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

您的更新功能,

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,
    ));
}