Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
Yii set model attributes and save

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,
        ));
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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,
        ));
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda