How to get count of related collection #1120
-
I need something that could probably be called I think it might be general enough to be added to IHP - but not sure how to approach it 😸 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
One good way to solve this is by adding a Then you can use This also has the advantage that it keeps your application response times fast. Without having a |
Beta Was this translation helpful? Give feedback.
One good way to solve this is by adding a
comments_count : Int
field to theposts
table. Then whenever a new comment is created, update thecomments_count
of the related field.Then you can use
get #commentsCount post
to access it from the view.This also has the advantage that it keeps your application response times fast. Without having a
comments_count
field, theCOUNT()
queries would get slower over time as the comments in the database are growing. With thecomments_count
field the runtime is independent of the amount of comments that exist.