Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

79
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,
        ));
}
8 months 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,
        ));
}
8 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.