CakePHP 3.0 第二组查询

CakePHP 3.0 Group Two Queries

对 cakePHP 还是新手,使用 SQL 还是有点慢...

我已经设法使用 cakephp 的查询生成器来优化我的查询,几乎在我想要的地方。我试过使用 ->group 和 ->select 的各种组合,但我似乎无法正确组合这两个查询。任何人都可以提供线索或新的(更合适的)方向吗?

我想要的查询:

SELECT
    accounts.id,
    accounts.account_name,
    account_credit_id,
    SUM(credit - debit)
FROM 
    (SELECT 
    account_credit_id,
    credit,
    debit    
    FROM transactions
    UNION
    SELECT 
    account_credit_id,
    credit,
    debit    
    FROM splits)
AS temp
INNER JOIN accounts
ON account_credit_id=accounts.id
WHERE accounts.account_term_id = 3
GROUP BY account_credit_id 
;

控制器(一团糟,无法正常工作):

    $expense_balances = $this->Transactions->find('all')
        ->contain(['AccountCredits'])
        ->where(['AccountCredits.account_term_id ' => 3]) //Only Expense Accounts
        ->group(['account_credit_id'])
        ->select(['total' => 'sum(credit - debit)' , 'account_credit_id' , 'account_name' => 'AccountCredits.account_name' ])   ;
    $split_expense_balances = $this->Transactions->Splits->find('all')
        ->contain(['Accounts'])
        ->where(['Accounts.account_term_id ' => 3]) //Only Expense Accounts
        ->group(['account_credit_id'])
        ->select(['total' => 'sum(credit - debit)' , 'account_credit_id' ,  'account_name' => 'Accounts.account_name' ])    ;

    $all_balances = $expense_balances->union($split_expense_balances)
            ->group(['account_credit_id']);

    debug($all_balances->toArray());

调试(您可以看到记录按 'account_credit_id' 分组,但有两组组 — 每个 table 一组):

[
(int) 0 => object(App\Model\Entity\Transaction) {

    'total' => '-72.5',
    'account_credit_id' => (int) 4,
    'account_name' => 'Pets',
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Transactions'

},
(int) 1 => object(App\Model\Entity\Transaction) {

    'total' => '-80',
    'account_credit_id' => (int) 5,
    'account_name' => 'Groceries',
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Transactions'

},
(int) 2 => object(App\Model\Entity\Transaction) {

    'total' => '-389.44998931884766',
    'account_credit_id' => (int) 2,
    'account_name' => 'Dining',
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Transactions'

},
(int) 3 => object(App\Model\Entity\Transaction) {

    'total' => '-118.77000045776367',
    'account_credit_id' => (int) 4,
    'account_name' => 'Pets',
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Transactions'

},
(int) 4 => object(App\Model\Entity\Transaction) {

    'total' => '-98.91999816894531',
    'account_credit_id' => (int) 5,
    'account_name' => 'Groceries',
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Transactions'

}

]

我想要达到的目标(按 'account_credit_id' 分组并求和):

[
(int) 0 => object(App\Model\Entity\Transaction) {

    'total' => '-191.27',
    'account_credit_id' => (int) 4,
    'account_name' => 'Pets',
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Transactions'

},
(int) 1 => object(App\Model\Entity\Transaction) {

    'total' => '-178.92',
    'account_credit_id' => (int) 5,
    'account_name' => 'Groceries',
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Transactions'

},
(int) 2 => object(App\Model\Entity\Transaction) {

    'total' => '-389.44998931884766',
    'account_credit_id' => (int) 2,
    'account_name' => 'Dining',
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Transactions'

},

]

为什么要奋斗? Cakephp 的文档指出,您最好直接执行复杂的查询。所以我做到了!

    $expense_balances = $connection->execute(
    'SELECT
        accounts.id,
        accounts.account_name,
        account_credit_id,
        SUM(credit - debit)
    FROM 
        (SELECT 
        account_credit_id,
        credit,
        debit    
        FROM transactions
        UNION
        SELECT 
        account_credit_id,
        credit,
        debit    
        FROM splits)
    AS temp
    INNER JOIN accounts
    ON account_credit_id=accounts.id
    WHERE accounts.account_term_id = 3
    GROUP BY account_credit_id '
    )
    ->fetchAll('assoc');