Logging user actions into database

Hi. I need some ideas and suggestions on how to implement this.

Basically I want to log certain user actions into database and presenting it to the administrator. For example, if user added a new post, the activity will be added into the database. You can say it’s app audit log but less formal.

What I have now is somewhat elementary. Whenever the controller saves a record, it will call a method in a model to save the activity into the database. Like so:-

// PostController

if($model->validate() && $model->save(false)) {

\app\models\Activity::saveRecord($type, $activity);


Then in Activity Model’s saveRecord method, it’ll switch $type to determine what activity just occurred (e.g. new post, post updated, etc) and then save the activity accordingly.

Any comments, thoughts and suggestion to improve it? Thanks.

It is better to use a behavior.

Maybe this one? -> asinfotrack/yii2-audittrail

In any case, that kind of logic/code belongs in the model, not the controller, IMO.