I want to track changes in my table and save it in another table. For example, if someone edits my 'text' table, i save it in 'textchanges' table. My textchanges has 4 columns, id, textid(foreign key), old text, change date. What im trying to do, in my text controller near action update i create new textchange model and set attributes, but its not working.
public function actionUpdate($id)
{
$model = $this->loadModel($id);
if (isset($_POST['Text'])) {
$model->attributes = $_POST['Text'];
$textChange = new TextsChanges();
$textChange->setAttributes(array('TextId' => $model->text_id, 'TextBefore' => $model->text_text, 'ChangeDate' => new DateTime()));
if ($model->save())
$this->redirect(array('view', 'id' => $model->text_id));
$textChange->save();
}
$this->render('update', array(
'model' => $model,
));
}
You should assign old values before assigned to model, like below.
public function actionUpdate($id)
{
$model = $this->loadModel($id);
if (isset($_POST['Text'])) {
$textChange = new TextsChanges();
$textChange->setAttributes(array('TextId' => $model->text_id, 'TextBefore' => $model->text_text, 'ChangeDate' => new DateTime()));
$model->attributes = $_POST['Text'];
if ($model->save())
$this->redirect(array('view', 'id' => $model->text_id));
$textChange->save();
}
$this->render('update', array(
'model' => $model,
));
}