Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda