How does countercache determine number


I want to have a field that shows how many items are related in another table. Countercache looks promising on this.

Are the updates countercache does kind of like a currentvalue-1 and currentvalue+1? The documentation states that it doesn’t update the value if you do updateAll and deleteAll.
Or does it actually do a count of the rows in the result?

I’m asking because an external application inserts into the database daily. So after that I want to refresh the counter cache to keep the application quick. How can I trigger this refresh? By setting it dirty?

Perhaps Countercache is not the way to go here. And if so, what sort of construction would be best?

Thanks a lot in advance.


Counter cache does counterValue +/- (entities affected)

If you modify data outside of the framework you could have a cronjob that update the values periodically, to that you can use shells.

Note that you have to make the query, like

UPDATE articles
SET comment_count = (
    FROM comments
    GROUP BY article_id