So is your concern here that you're doing a separate query for each count? You should be able to accomplish this in a single query easily by manually joining the tables.
Use the query builder. Here's some info from the docs on joins.
I am developing a site at the moment which allows people to create articles in a blog format.
A feature of the site allows other users to 'like' and 'comment' on the article.
I am wanting to count the number of likes and comments an article has and display the count in a summary at the top of the article page. (A bit like the way Jeffrey displays the number of comments on a forum post in the right side of the listing page)
Currently, if a user comments on an article, I post the comment to the Comments table. Similarly, if a user 'likes' an article, I post the like to the Likes table. Using my current architecture, it means I have to query the comments table and get the count, then query the likes table for the count each time an article is viewed.
I am wondering if a better architecture would be to add 2 columns (Counts, Comments) to the Articles table and add/remove an increment each time a user adds/removes a like or comment? The idea would then be to simply display the count in the article without having to query or join other tables.
If anyone can think of another way to do this or thinks the way I have suggested is a bad idea then I would love to hear your feedback.
Thanks in advance
Please or to participate in this conversation.