O
O
Oversec2016-09-29 23:19:58
Yii
Oversec, 2016-09-29 23:19:58

How to implement ticket change history in Yii2?

There was such a need to keep track of who changes applications. While it is implemented like this: there is a logs table, we save it when accessing the controller for changes:

public function actionComment()
    {
        $log = new Logs();
        $log->task_id = $id;
        $log->user_id = Yii::$app->user->id;
        $log->time = date('Y-m-d H:i:s');
        $log->new_data = $comment;
        $log->action = 'Комментарий';

        if(!$log->save(false)) {
            return false;
        }

//Далее сохраняем данные

But a lot of work is being done. Is it possible to implement this more correctly?
Second question: How to learn what field changes in the table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-09-30
@Oversec

If you want to implement this functionality more correctly, then you should not shove it into the controller. he just doesn't belong there. The controller only needs to process the user's request, interact with the business logic and presentation layer.
Implement a separate component/behavior that will:
1. Catch data saving events in models
2. Get record objects themselves from these events
3. Remove changed data from the object using the getDirtyAttributes() method 4. Form
the log object itself and save it
but before With this, I would recommend you to look for ready-made solutions, because. for this task - they are

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question