0

このSQL構文に従って、CakePHPでクエリを結合する方法を知りたいだけです

SELECT a.id, SUM( r.jumlah_realisasi) AS jumlah_realisasi, SUM(b.jumlah_budget) AS jumlah_budget, SUM(c.jumlah_contrapos)
FROM accountposts a
LEFT JOIN
(
SELECT accountpost_id, agency_id, SUM(budget_after_changes) AS jumlah_budget
FROM budgets
GROUP BY accountpost_id
) b
ON a.id = b.accountpost_id
LEFT JOIN
(
SELECT accountpost_id, agency_id, SUM(realisation_value) AS jumlah_realisasi
FROM realisations
GROUP BY accountpost_id
) r
ON a.id = r.accountpost_id
LEFT JOIN
(
SELECT accountpost_id, agency_id, SUM(contrapos_value) AS jumlah_contrapos
FROM contraposts
GROUP BY accountpost_id
) c
ON a.id = c.accountpost_id
GROUP BY
a.id

そして、私はこの構文で試しました(CakePHP 2.xを使用しています):

$joins = array(
               array(
                  'table' => 'budgets',
                  'alias' => 'Budget',
                  'type' => 'LEFT',
                  'conditions' => array('Accountpost.id = Budget.accountpost_id')
                ),
                array(
                  'table' => 'realisations',
                  'alias' => 'Realisation',
                  'type' => 'LEFT',
                  'conditions' => array('Accountpost.id = Realisation.accountpost_id')
                ),
                array(
                   'table' => 'contraposts',
                   'alias' => 'Contrapost',
                   'type' => 'LEFT',
                   'conditions' => array('Accountpost.id = Contrapost.accountpost_id')
                ),
         );

        $this->paginate = array(
            'limit' => 60,
            'joins' => $joins,
            'fields' => array('Accountpost.id','Accountpost.explanation','Accountpost.account_code',
                              'SUM(Budget.budget_after_changes) AS jumlah_budget','SUM(Realisation.realisation_value) AS jumlah_realisasi','SUM(Contrapost.contrapos_value) AS jumlah_contrapos'),
            'group' => array('Accountpost.id'),
            'order' => array('Accountpost.id' => 'ASC'),
        );

そして、ここに CakePHP からの SQL ダンプがあります:

SELECT `Accountpost`.`id`, `Accountpost`.`explanation`, `Accountpost`.`account_code`, SUM(`Budget`.`budget_after_changes`), `Budget`.`budget_after_changes`, `Realisation`.`realisation_value`, `Contrapost`.`contrapos_value` FROM `realisasi_anggaran`.`accountposts` AS `Accountpost` LEFT JOIN `realisasi_anggaran`.`budgets` AS `Budget` ON (`Accountpost`.`id` = `Budget`.`accountpost_id`) LEFT JOIN `realisasi_anggaran`.`realisations` AS `Realisation` ON (`Accountpost`.`id` = `Realisation`.`accountpost_id`) LEFT JOIN `realisasi_anggaran`.`contraposts` AS `Contrapost` ON (`Accountpost`.`id` = `Contrapost`.`accountpost_id`) WHERE 1 = 1 GROUP BY `Accountpost`.`id` ORDER BY `Accountpost`.`id` ASC LIMIT 60

ただし、SQL 構文版と CakePHP 版では結果が異なり、SQL 構文では SUM を調べる際に値が重複しませんが、CakePHP 版では SUM を調べる際に値が重複します。CakePHP に SQL 構文を正しく実装するにはどうすればよいですか?

4

1 に答える 1

0

$options配列が正しく構築されていません。このように、配列には 2 つのネストされた名前付きキーが呼び出されjoins、Cake は正しい SQL 結合を構築しません。

配列は次のようになります。

$joins = array(
  array(
    'table' => 'budgets',
    'alias' => 'Budget',
    'type' => 'LEFT',
    'conditions' => array('Accountpost.id = Budget.accountpost_id')
  )
);

$this->paginate = array(
  'limit' => 60,
  'joins' => $joins,
  'fields' => array('Accountpost.id','Accountpost.explanation','Accountpost.account_code',
                                  'SUM(Budget.budget_after_changes) AS jumlah_budget'),
  'group' => array('Accountpost.id'),
  'order' => array('Accountpost.id' => 'ASC'),
);

または、次のように構成されます。

$options = array(
  'limit' => 60,
  'fields' => array('Accountpost.id','Accountpost.explanation','Accountpost.account_code',
                                  'SUM(Budget.budget_after_changes) AS jumlah_budget'),
  'group' => array('Accountpost.id'),
  'order' => array('Accountpost.id' => 'ASC'),
);

$options['joins'] = array(
  array(
    'table' => 'budgets',
    'alias' => 'Budget',
    'type' => 'LEFT',
    'conditions' => array('Accountpost.id = Budget.accountpost_id')
  )
);

$this->paginate = $options;
于 2012-07-29T13:26:30.057 に答える