Use the Find (' count ') method in cakephp

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.