A calculated value is a read only property that is calculated using sql expressions. In this example we are building a blog application that has posts and comments. First we have a post entity. I have stripped these down for this example so we can look at the root of the issue. And then we have a comment entity.
So far this is pretty basic stuff, nothing really special going on here. Now we need to grab a record from our database and display it, easy enough. There is a major problem here with our display though. How would we get a count of the number of comments for this post? You could count the array of comments. While this would work your going to have to do this 2x on the post display page now. One time for the comment count and then a 2nd time to display the comments. In a post where there is only 1 or 2 comments this will not be a big issue but you can imagine the trouble this would cause with a large number of comments. A better solution to this problem is to use a calculated value. We can actually create this commentCount as a property of our post entity. All we are doing here using SQL to the count. The id of the Post Entity is id (comes from the base Entity) so we can do a lookup in the comment table based on post_id being equal to id. Now if we want to output the count all we have to do is.
