繁体   English   中英

cakephp-按年,月分组

[英]cakephp - using group by year, month

我有三个表,分支机构,子级和发票-子级hasmany发票。 和分支机构有很多孩子

我试图允许用户查询数据库以获取给定分支中所有子代的发票,而且用户可能已经输入了日期范围。 无论哪种方式,我都希望选择所有发票并将其分组,以便易于在每个月的表格中显示它们。 也就是说,用户想要从2013年1月到2014年1月的所有发票。然后显示所有发票,但将它们分组,以便所有jan2013在一起,feb2013等...(此外,如果很容易获得发票的总和,每个月以某种方式会很酷)

此刻我有:

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

但是它只返回2条记录,而应该返回所有5条记录。我注意到我的5张发票中只有2个月,所以我猜我的错误代码每月仅获得一张发票。

谁能告诉我如何获得想要的结果?

如果我了解您要正确执行的操作,则是在寻找订单,而不是分组。 分组依据将使所有给定列具有相同结果的所有行压缩为一行,从而使结果中看到的行减少。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM