Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!