Hits count

I have a table field hits in my database, i want it to be increased every time a user watches the particular table item in UI. How can i do it?


You can insert view id in another table with HAS_MANY relation at every view time then you can count as SUM to display Count Hits.


For example, you can do it this way.





actually there is much better way to do it (save() involves validation and postting ALL field values again to db).

better way:


$model->saveCounters(array('hits'=>1));  //this means: update 'hits' attribute by 1 and save only that attribute.

…or if you do not need to load the model at all:



        "id = :id", // or another, broader condition

        array(':id' => $id),


more: http://www.yiiframework.com/wiki/282/using-counters-with-activerecord/