php CakePHP 和 GROUP BY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2929666/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:10:05  来源:igfitidea点击:
CakePHP and GROUP BY
提问by Lizard
Using CakePHP 1.2, I am trying produce a GROUP BYquery:
使用 CakePHP 1.2,我正在尝试生成一个GROUP BY查询:
SELECT `categories`.*, COUNT(`entities`.id)
FROM `categories` 
LEFT JOIN `entities` ON (`categories`.`id` = `entities`.`category_id`)
GROUP BY `categories`.`id`
How would/should I go about doing this? I am using 'Containable' if that helps.
我将/应该如何做这件事?如果有帮助,我正在使用“可容纳”。
回答by Lizard
This is what I eneded up with:
这就是我所想到的:
 $options = array(
                    'conditions' => $conditions,
                    'fields'=>array('Category.*','COUNT(`Entity`.`id`) as `entity_count`'),
                    'joins' => array('LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id`'),
                    'group' => '`Category`.`id`',
                    'contain' => array('Domain' => array('fields' => array('title')))
                );
                return $this->find('all', $options);
回答by webbiedave
Model->find()has a groupparam.
Model->find()有一个group参数。
回答by Aditya P Bhatt
Possible keys by default, all of which are optional:
默认情况下可能的键,所有这些都是可选的:
$params = 
        array(
        'conditions' => array('Model.field' => $thisValue), //array of conditions
        'recursive' => 1, //int
        //array of field names
        'fields' => array('Model.field1', 'DISTINCT Model.field2'),
        //string or array defining order
        'order' => array('Model.created', 'Model.field3 DESC'),
        'group' => array('Model.field'), //fields to GROUP BY
        'joins' => array('Join relations here'), // for ex: LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id`
        'limit' => n, //int
        'page' => n, //int
        'offset' => n, //int
        'callbacks' => true //other possible values are false, 'before', 'after'
    );
Query:
询问:
$resp = find('all', $params);
debug($resp);

