0

私はcakephp 1.3を使用しています。収入テーブルに日付フィールドがあります。ループで月ごとの総収入を取得しようとしています。以下は私の質問です。

$income_and_hst = $Income->find('all', array(
                            'fields' => array('SUM(Income.amount) as income', 
                                            'SUM(Income.hst_amount) as hst', 
                                            'MONTH(date) as month'),
                            'conditions' => array(
                                'Income.income_account_id' => array(1,2,3,4),
                                'Income.user_id' => $this->Auth->user('id'),
                                'Income.account_id' => $this->Session->read('Account.default_account'),
                                'Income.date >' => $starting_date,
                                'Income.date <' => $ending_date,
                                ),
                            'group' => 'MONTH(date)',
                            )
                        );

これで5か月分の収入が得られます。収入は5ヶ月からだったからです。他の月に収入がない場合でも、12 か月すべてを表示する必要があります。収入がない場合は、その月に 0 が必要です。

誰かが私に方向を教えてもらえますか?

ありがとうございました。

4

1 に答える 1

0

私の推測では、そのインターフェイスを使用してそれを行うことはできません。生の SQL (およびクロス結合) に飛び込むか、不足している月を周囲の php コードに追加する必要があります。後者のようなものがうまくいくはずです(疑似コード、php構文を覚えていません):

for ($row in $income_and_hst) {
    $income_and_hst_all[$row['month']] = $row
}

for ($month = 1;$month <= 12; $month++) {
    if ($income_and_hst_all[$month] == nil) {
       $income_and_hst_all[$month] = Array (
           'income' => 0, 
           'hst' => 0, 
           'month' => $month
       )
    }
}
于 2013-09-28T23:00:33.170 に答える