Quiero realizar un seguimiento de los cambios en mi tabla y guardarlos en otra tabla. Por ejemplo, si alguien edita mi tabla de 'texto', la guardo en la tabla de 'cambios de texto'. Mis cambios de texto tienen 4 columnas, id, textid (clave externa), texto antiguo, fecha de cambio. Lo que estoy tratando de hacer, en mi controlador de texto cerca de la actualización de acción, creo un nuevo modelo de cambio de texto y establezco atributos, pero no funciona.
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, )); }
Debe asignar valores antiguos antes de asignarlos al modelo, como se muestra a continuación.
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, )); }