Cakephp CounterCache not update field value

Hi!.. everyone
I am try to update point_count value in articles table when i call custom Finder Method from Articles table model…

CounterCache Behavior initialize in Articlestable

Custom Finder in same ArticleTable

Somewhere in Controller side Call Custom Finder Method

But points not updating whenever i am calling this finder method!! May Do something wrong here?

CounterCache doesn’t update a table when the finder is called. That would defeat the purpose, as it would have to run the query each time you read! My question here is what are you trying to count? Where do article “points” come from? The CounterCacheBehavior needs to be added to that table, then each time a record is saved there, it updates the cache field on the Articles table.

1 Like

Yes, you can provide a finder that will be used to calculate the counter, when the counter needs to be calculated. This does not mean that every usage of this finder will update the counter.

oh! ok. so how can i update the visitor view count who read article on authors page? i have field view_count in article table and i want to increment view_count when user go to the particular authors blog and starting read article then i want to update view_count in article table, that why I just created custom finder method and set CounterCacheBehavior in Article table same as above docs show us… but when custom finder method execute i mean when custom finder method fetching data then on the same time CounterCacheBehavior should be increment view_count but it does not work as you told above it does not work so how does it work. if you any code example please provide information bro @Zuluru

That’s not at all what the counter cache is for. From the counter cache manual page:

Often times web applications need to display counts of related objects. For example, when showing a list of articles you may want to display how many comments it has. Or when showing a user you might want to show how many friends/followers she has. The CounterCache behavior is intended for these situations.

You are not lookig for a “count of related objects”, you’re looking for a “count of views”. There is nothing built-in to Cake that will do this for you. You’ll need to put something in your ArticlesController view function to do this. Very simply, just add 1 to the view_count on the entity you’ve read, and save it again.

Now, if you wanted to do something that would cache the number of reads of all articles for each author, that becomes something CounterCache could help with. In the Articles table, you’d put a CounterCache for the Authors table, then each time an article is saved, the counter on the article’s author would be updated.