Welcome to the Cookbook

loading...

3.7.3.1.2 find('count')

find('count', $params)

find('count', $params) は整数型の値を返します。コントローラ中で使う場合の簡単な例を、いくつか次に示します。

function some_function() {
   ...
   $total = $this->Article->find('count');
   $pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));
   $authors = $this->Article->User->find('count');
   $publishedAuthors = $this->Article->find('count', array(
      'fields' => 'COUNT(DISTINCT Article.user_id) as count',
      'conditions' => array('Article.status !=' => 'pending')
   ));
   ...
}
  1. function some_function() {
  2. ...
  3. $total = $this->Article->find('count');
  4. $pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));
  5. $authors = $this->Article->User->find('count');
  6. $publishedAuthors = $this->Article->find('count', array(
  7. 'fields' => 'COUNT(DISTINCT Article.user_id) as count',
  8. 'conditions' => array('Article.status !=' => 'pending')
  9. ));
  10. ...
  11. }

find('count') では、 fields に配列を渡してはいけません。DISTINCT カウントを行うフィールドだけを指定するようにしてください。そうすることで、条件に従った結果が常に同じになります。

find('count') において使用する追加のパラメータは、前述したもので全てです。