How to create user activity reports?


I would like to log user’s activity on my site. To be more specific, I would like to store in DB what kind of changes users did with which controller/action and I want to show these activity logs to them (so they cant say they didn’t change anything).

Because my project is already big enough, with a lots of controllers and models I look for a general solution, that means, I don’t want to write a function call (which will make the log) into ~100 different actions.

I tried to make this via custom controller filters and with the help of before/afterAction. The problem is that when a user do something first the action will render the view (a new record to logs table) and if it has a form for example then it will run again (another record in the logs table with same controller/action). Another problem is that I cant track If they did changes to the models or not (action was successful or not).

So instead of these practices, I made a behavior, which has an afterSave function and put it inside of the ActiveRecord class. With this I can track all of the changes made to models BUT my problem is, there are many actions which modify more than 1 models, so for one action I store more than 1 records and because of that I cant show these logs to the users.

For example a user add a new topic to the forum with a message in it, this action create 2 new AR models (means 2 records in the logs table), but I only want to show to the user that he creates a new topic (the first log).

Is there any solution for this?

/Sorry for my bad English/

you could create beforeAction handler (you may attach one to all controllers either in base controller class) or onBeginRequest handler in configuration file. there you may log user id, request method (GET/POST) and action/url… you can also add afterSave handlers in your models to log exact model changes…

Thanks Maciej Liżewski for your reply. As I mentioned before I already implemented this functionality with beforeAction and afterSave handlers. The problem is that I don’t want to show all model updates/changes and all function calls to the users so I have to assort these records somehow?!

Is it possible to get a list of the modified/created models group by the handler action? If I wasn’t clear enough here is an example:

public function actionModifyModels()


        $model1 = new Model();

        $model2 = new Model();

        $model1->attribute1 = 'something';

        $model2->attribute1 = 'someting else';


        if ($model1->save()) {





I would like to get a list of modified models group by the action, so based on this example: model1, model2 in modifyModels.

so beforeAction must store information about action invoked in some global state, like:

Yii::app()->params['_action'] = $action;

and in afterSave handler you refer to that state and save it with every entry:

$model->attributes = $this->attributes;

$model->on_action = Yii::app()->params['_action'];


I do not recommend refering straight to Yii::app()->request in afterSave because it will fail when you try to save models from commandline (command line application does not have ‘request’ component). This way commandline app can set something in that config param (like Yii::app()->params[’_action’] = ‘commandline’; or even straight in commandline config file)