I
I
ITwoolf2019-03-12 16:45:43
Yii
ITwoolf, 2019-03-12 16:45:43

How to force afterSave to save old data that has been changed in 1 plate to another?

Good afternoon. Here I have a Kniga1 plate with my Kniga1.php model. This table has its own fields. Description of the book, etc. I created a second plate so that if you change the description of the book in the first one, it will be saved in the second plate. I already figured out that I need to use the afterSave function.
public function afterSave($insert, $changedAttributes){
parent::afterSave($insert, $changedAttributes);
???
}
But what exactly should I prescribe in order for this connection to work? By the way, I made the usual standard connection between tables. one to many and many to one.
5c87b7b18c57d978739620.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
di, 2019-03-12
@ITwoolf

For example

public function beforeSave($insert)
{
    if (!parent::beforeSave($insert)) {
        return false;
    }
    $description = new Description();
    $description->book_id = $this->id;
    $description->old_text = $this->text;
    $description->created_at = time();
    $description->save();
    return true;
}

H
hostadmin, 2019-03-14
@hostadmin

If you need a history of changes, then look towards this component https://github.com/bupy7/yii2-activerecord-history
Just add the desired model to the behaviors and automatically its history is saved in a separate table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question