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.

#1 by Raymond Camden on 9/7/10 - 8:59 AM
#2 by Dan Vega on 9/7/10 - 9:07 AM
#3 by Dan Vega on 9/7/10 - 9:09 AM
#4 by Raymond Camden on 9/7/10 - 9:14 AM
#5 by Rick O on 9/7/10 - 9:23 AM
#6 by Terrence Ryan on 9/7/10 - 9:49 AM
Not a deal breaker for everyone, but be aware
#7 by Raymond Camden on 9/7/10 - 9:54 AM
#8 by Tony Garcia on 9/7/10 - 10:07 AM
#9 by Dan Vega on 9/7/10 - 10:10 AM
This will give you a random number each time.
#10 by Chris Blackwell on 9/7/10 - 10:11 AM
There is also a gotcha with this method too. A newly created instance of a persistent cfc does not contain an empty array (or struct) for its realtionship properties, they are actually undefined. So you shouldn't rely on getCooments() returning an array and should test with isNull().
My base persistent cfc contains a utility method for this purpose
<code>
numeric function getCommentCount() {
return countObjectsInRelationship("Comments");
}
private numeric function countObjectsInRelationship(required string key) {
if(structKeyExists(variables, key) AND !isNull(variables[key])) {
if(isArray(variables[key]))
return arrayLen(variables[key]);
else if(isStruct(variables[key]))
return structCount(variables[key]);
}
return 0;
}
</code>
#11 by Raymond Camden on 9/7/10 - 10:12 AM
#12 by Raymond Camden on 9/7/10 - 10:16 AM
#13 by Dan Vega on 9/7/10 - 10:18 AM
#14 by Chris Blackwell on 9/7/10 - 10:20 AM
at least, that was my understanding of how it worked....
#15 by Dan Vega on 9/7/10 - 10:24 AM
comment count
category count
posts by category count
first order date
last order date
days since last order
#16 by Raymond Camden on 9/7/10 - 10:26 AM
#17 by Mike Bosch - Software Engineer on 1/18/11 - 3:29 AM