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?
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?
Hello,
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.
Thanks
For example, you can do it this way.
$model=ModelName::model()->findByPk($rowId);
$model->hits+=1;
$model->save();
actually there is much better way to do it (save() involves validation and postting ALL field values again to db).
better way:
$model=ModelName::model()->findByPk($rowId);
$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:
ModelName::model()->updateCounters(
array('hits'=>1),
"id = :id", // or another, broader condition
array(':id' => $id),
);
more: http://www.yiiframework.com/wiki/282/using-counters-with-activerecord/