Back
count()
Returns the number of rows that match the arguments (or all rows if no arguments are passed in).
Uses the SQL function COUNT
.
If no records can be found to perform the calculation on, 0
is returned.
// Count how many authors there are in the table authorCount = model("author").count(); // Count how many authors that have a last name starting with an "A" authorOnACount = model("author").count(where="lastName LIKE 'A%'"); // Count how many authors that have written books starting with an "A" authorWithBooksOnACount = model("author").count(include="books", where="booktitle 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 aPost = model("post").findByKey(params.postId); amount = aPost.commentCount();
Copied!