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

对于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')方法

时间: 2024-08-27 18:48:45

cakephp中使用 find('count')方法的相关文章

Python List count()方法-用于统计某个元素在列表中出现的次数

描述 count() 方法用于统计某个元素在列表中出现的次数. 语法 count()方法语法: list.count(obj) 参数 obj -- 列表中统计的对象. 返回值 返回元素在列表中出现的次数. 实例 以下实例展示了 count()函数的使用方法: #!/usr/bin/python aList = [123, 'xyz', 'zara', 'abc', 123]; print "Count for 123 : ", aList.count(123); print "

谈谈map中的count方法

map和set两种容器的底层结构都是红黑树,所以容器中不会出现相同的元素,因此count()的结果只能为0和1,可以以此来判断键值元素是否存在(当然也可以使用find()方法判断键值是否存在). 拿map<key,value>举例,find()方法返回值是一个迭代器,成功返回迭代器指向要查找的元素,失败返回的迭代器指向end.count()方法返回值是一个整数,1表示有这个元素,0表示没有这个元素. #include<iostream> #include<map> #i

oc中字典的实现方法详解

一:字典的基本概念 Foundation中的字典(NSDictionary,NSMutableDictionary)是由键-值对组成的数据集合.正如,我们在字典里查找单词的定义一样. 通过key(键),查找的对应的value(值),key通常是字符串对象,也可以是其他任意类型对象.在一个字典对象中,key的值必须是唯一的. 此外,字典对象的键和值不可以为空(nil),如果需要在字典中加入一个空值,可以加入NSNull对象 二:不可变字典-NSDictionary 1:初始化(以一个元素和多个元素

iOS中数组遍历的方法及比较

数组遍历是编码中很常见的一种需求,我们来扒一拔iOS里面都有什么样的方法来实现,有什么特点. 因为iOS是兼容C语言的,所以C语言里面的最最常见的for循环遍历是没有问题的. 本文中用的数组是获取的系统的语言数组,大约有30多个数据,虽然还不够模拟大批量的数据,但对于方法的验证是没有问题的了. NSArray *langArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"AppleLanguages"]; 第一种方法

iOS开发中自定义字体的方法

http://www.cnblogs.com/iyou/archive/2014/05/25/3751669.html 1. 首先下载你想要设置的字体库,例如设置方正启体简体 2. 添加到工程,一定要注意勾选红色框框处,默认是不勾选的  添加以后 3.在plist文件中添加 4.现在已经添加成功了,但是要使用就必须知道FontName,用以下代码可查到 NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyName

IOS 开发中遍历数组的方法及比较

数组,做为一种常用的数据类型,频繁出现在编码中,其中肯定少不了对数组的遍历,本博文对数组遍历,进行一下自己的归纳,如果是大牛,一笑而过就好,互相学习,欢迎指正. 话不多说直接进入主题 首先创建一个数组 /** 获取系统的语言数组 */ NSArray *languageArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"AppleLanguages"]; for 循环-C语言 因为在xCode 编译中支持C 语言,

JS中setTimeout()的使用方法具体解释

1. SetTimeOut()              1.1 SetTimeOut()语法样例              1.2 用SetTimeOut()运行Function              1.3 SetTimeOut()语法样例              1.4 设定条件使SetTimeOut()停止              1.5 计分及秒的counter    2. ClearTimeout()    3.  Set Flag   10.1 setTimeout( )

C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:

public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); 将指定数目的字节从起始于特定偏移量的源数组复制到起始于特定偏移量的目标数组. /// <summary> /// C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法 /// </summary> /// <param name="str&

asp.net中导出Execl的方法

一.asp.net中导出Execl的方法: 在 asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址 输出在浏览器上:一种是将文件直接将文件输出流写给浏览器.在Response输出时,\t分隔的数据,导出 execl时,等价于分列,\n等价于换行. 1.将整个html全部输出execl 此法将html中所有的内容,如按钮,表格,图片等全部输出到Execl中.   Response.Clear();       Response.Buffer=