For find (' Count ', Array (' group ' = ' user_id '));
This is described in model.php:
1 /**2 * Handles The Before/after filter logic for Find (' count ') operations. Only called by Model::find ().3 *4 * @param string $state either "before" or "after"5 * @param array $query6 * @param array $results7 * @return Integer The number of records found, or false8 * @see model::find ()9 */Ten protected function_findcount ($state,$query,$results=Array()) { One if($state= = = ' before ') { A if(!Empty($query[' type ']) &&isset($this->findmethods[$query[' type ']]) &&$query[' type ']!== ' count ') { - $query[' operation '] = ' count '; - $query=$this->{' _find '.Ucfirst($query[' type '])} (' Before ',$query); the } - - $db=$this-Getdatasource (); - $query[' order '] =false; + if(!method_exists($db, ' Calculate ')) { - return $query; + } A at if(!Empty($query[' Fields ']) &&Is_array($query[' Fields '])) { - if(!Preg_match('/^count/i ', Current($query[' Fields ']))) { - unset($query[' Fields ']); - } - } - in if(Empty($query[' Fields '])) { - $query[' fields '] =$db->calculate ($this, ' Count '); to}ElseIf(method_exists($db, ' expression ') &&is_string($query[' Fields ']) &&!Preg_match('/count/i ',$query[' Fields '])) { + $query[' fields '] =$db->calculate ($this, ' Count ',Array( - $db->expression ($query[' Fields ']), ' count ' the )); * } $ Panax Notoginseng return $query; - } the + foreach(Array(0,$this->alias) as $key) { A if(isset($results[0] [$key[' Count '])) { the if($query[' Group ']) { + return Count($results); - } $ $ return intval($results[0] [$key[' Count ']); - } - } the - return false;Wuyi}
In the cakephp test case, there is a description:
1 $expected=Count($Article->find (' All ',Array(2' Fields ' =Array(' article.user_id '),3' Conditions ' =Array(' article.user_id ' = 1),4' Group ' = ' article.user_id ')5 ));6 $result=$Article->find (' Count ',Array(7' Conditions ' =Array(' article.user_id ' = 1),8' Group ' =Array(' article.user_id '),9));
The results of $expected and $result are the same. It is also possible to use count,group statistics.
Using Find (' Count ', Array (' fields ' = ' distinct user_id ')), the effect is the same.
Use the Find (' count ') method in cakephp