count()
Description
Returns the number of rows that match the arguments (or all rows if no arguments are passed in). Uses the SQL function COUNT.
Function Syntax
count([ where, include ])
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
where |
string |
No | |
An SQL fragment such as lastName LIKE 'A%' for example. |
include |
string |
No | |
Any associations that need to be included in the query. |
Examples
<!--- Count how many authors there are in the table --->
<cfset authorCount = model("author").count()>
<!--- Count how many authors whose last name starts with "A" there are --->
<cfset authorOnACount = model("author").count(where="lastName LIKE 'A%'")>
<!--- Count how many authors that have written books starting on "A" --->
<cfset authorWithBooksOnACount = model("author").count(include="books", where="title LIKE 'A%'")>
<!--- Count the number of comments on a specific post (a `hasMany` association from `post` to `comment` is required) --->
<!--- The `commentCount` method will call `model("comment").count(where="postId=#post.id#")` internally --->
<cfset aPost = model("post").findByKey(params.postId)>
<cfset amount = aPost.commentCount()>
