cakephp中使用 find('count')方法

來源:互聯網
上載者:User

標籤:

對於find(‘count‘,array(‘group‘=>‘user_id‘));

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 $query 6  * @param array $results 7  * @return integer The number of records found, or false 8  * @see Model::find() 9  */10     protected function _findCount($state, $query, $results = array()) {11         if ($state === ‘before‘) {12             if (!empty($query[‘type‘]) && isset($this->findMethods[$query[‘type‘]]) && $query[‘type‘] !== ‘count‘) {13                 $query[‘operation‘] = ‘count‘;14                 $query = $this->{‘_find‘ . ucfirst($query[‘type‘])}(‘before‘, $query);15             }16 17             $db = $this->getDataSource();18             $query[‘order‘] = false;19             if (!method_exists($db, ‘calculate‘)) {20                 return $query;21             }22 23             if (!empty($query[‘fields‘]) && is_array($query[‘fields‘])) {24                 if (!preg_match(‘/^count/i‘, current($query[‘fields‘]))) {25                     unset($query[‘fields‘]);26                 }27             }28 29             if (empty($query[‘fields‘])) {30                 $query[‘fields‘] = $db->calculate($this, ‘count‘);31             } elseif (method_exists($db, ‘expression‘) && is_string($query[‘fields‘]) && !preg_match(‘/count/i‘, $query[‘fields‘])) {32                 $query[‘fields‘] = $db->calculate($this, ‘count‘, array(33                     $db->expression($query[‘fields‘]), ‘count‘34                 ));35             }36 37             return $query;38         }39 40         foreach (array(0, $this->alias) as $key) {41             if (isset($results[0][$key][‘count‘])) {42                 if ($query[‘group‘]) {43                     return count($results);44                 }45 46                 return intval($results[0][$key][‘count‘]);47             }48         }49 50         return false;51     }

在cakephp測試案例中,有這樣的描述:

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         ));

 

$expected 和 $result 的結果是一樣的。那麼使用count,group統計也是可行的。

使用find(‘count‘,array(‘fields‘=>‘distinct user_id‘));效果也是一樣的。

cakephp中使用 find('count')方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.