问题描述
|
例如,我有表格-Orders1,Orders2,Orders3,Users。
我需要为每个用户制作每月和每季度的报告。例如,我正在Orders1模型
quartal_sum_by_users($users,$quarter)
中创建一个函数。然后我做这样的事情:
$cost = 0;
foreach($users as $user) {
while($month <= 3) { // first three months
$orders = $this->Order1->find(\'all\',array(
\'recursive\' => -1,\'fields\' => array(\'DISTINCT id\',\'COALESCE(cost+work_cost+work_installation_cost,0) AS amount\'),\'conditions\' => array(
\'MONTH(date_ordered)\' => $month,\'YEAR(date_ordered)\' => $year,\'current_status\' => 3
)
));
foreach($orders as $order) {
$cost[$month] += $order[0][\'amount\'];
}
$month++;
} // WHILE
} // FOREACH
因此,这是可行的,但速度非常慢(我正在迁移的系统正在处理普通的PHP和SQL查询,并且运行良好),我有400多个用户,每个Order表超过1000个。
使它成为CakePHP风格的最佳方法是什么?
解决方法
解决方案应作为答案而不是评论。否则,此问题将永远标记为未回答。
无论如何...这是您需要按以下分组的工作:
$this->Order1->find(\'all\',array(
\'recursive\' => -1,\'fields\' => \'YOUR_FIELDS\',\'conditions\' => \'YOUR_CONDITIONS\',\'group\' => \'THE_FIELD_YOU_WANT_TO_USE_AS_GROUP\'
));
选项组接受一个数组,因此您可以按多个字段进行分组。
对于更复杂的查询:book.cakephp.org/view/1030/Complex-Find-Conditions