Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cakephp-按年、月分组使用_Php_Mysql_Sql_Cakephp - Fatal编程技术网

cakephp-按年、月分组使用

cakephp-按年、月分组使用,php,mysql,sql,cakephp,Php,Mysql,Sql,Cakephp,我有三张桌子、分行、子公司和发票——子公司有很多发票。他有很多孩子 我试图允许用户查询数据库,以获取给定分支中的所有子级发票,并且用户可能已经输入了日期范围。无论采用哪种方式,我都希望选择所有发票并对其进行分组,以便每月在表中轻松显示。即,用户需要2013年1月->2014年1月的所有发票。然后我显示所有发票,但分组后所有2013年1月、2013年2月等都在一起。。。(另外,如果有一个简单的方法可以得到每个月的发票总额,那就很酷了) 目前我有: $conditions['Child.branch

我有三张桌子、分行、子公司和发票——子公司有很多发票。他有很多孩子

我试图允许用户查询数据库,以获取给定分支中的所有子级发票,并且用户可能已经输入了日期范围。无论采用哪种方式,我都希望选择所有发票并对其进行分组,以便每月在表中轻松显示。即,用户需要2013年1月->2014年1月的所有发票。然后我显示所有发票,但分组后所有2013年1月、2013年2月等都在一起。。。(另外,如果有一个简单的方法可以得到每个月的发票总额,那就很酷了)

目前我有:

$conditions['Child.branch_id'] = $branch_id;
if($from != '') $conditions['Invoice.created >='] = $from;
if($to != '') $conditions['Invoice.created <='] = $to;
$fields = array('Invoice.*', 'Child.*', 'YEAR(Invoice.created) as year', 'MONTH(Invoice.created) as month');
$group = array('YEAR(Invoice.created)', 'MONTH(Invoice.created)');
$order = array('Invoice.created');
$kidsInvoices = $this->Invoice->find('all', array(  'conditions'=>$conditions,
                                            'fields'=>$fields,
                                            'group'=>$group,
                                            'order'=>$order));
$conditions['Child.branch\u id']=$branch\u id;
如果($from!='')$conditions['Invoice.created>=']=$from;

如果($to!='')$conditions['Invoice.created如果我理解您希望正确执行的操作,则您正在查找的是订单,而不是分组。分组方式将使具有相同结果的所有行压缩到一行,从而减少您在结果中看到的行数

 $order = array('year', 'month');
 $kidsInvoices = $this->Invoice->find('all', array(  'conditions'=>$conditions,
                                        'fields'=>$fields,
                                        'order'=>$order));

嗨,Kai,谢谢你的回答。我一直在更多地考虑你的建议。我想你可能是对的,我可以这样做,但我想我需要循环一次结果来把它整理出来。我希望能有一个查询,给我像$results[2013][10][Invoice]这样的结果