BlackAlpha
BlackAlpha

Reputation: 376

Problems with SUM() and alias with CakePHP 3

I'm trying to do a SELECT with a SUM but I have a probleme with aliases. CakePHP 3 is used here.

In my controller, I do :

$preparations->find('all',
                [   'fields' => ['SUM(Preparations.qty) as sumqty', 'order_id', 'product_id'],
                    'conditions' => ['order_id IN ' => $ids],
                    'contain' => ['Products'],
                    'group' => 'product_id'
                ]);

But the query i have with this is :

SELECT SUM(Preparations.qty) as sumqty AS SUM(`Preparations__qty`) AS `sumqty`, Preparations.order_id AS `Preparations__order_id`, Preparations.product_id AS `Preparations__product_id`

SUM() is written twice. How can I resolve this problem ?

Upvotes: 0

Views: 377

Answers (1)

Bart
Bart

Reputation: 1268

Try this

'fields' => ['sumqty'=>'SUM(Preparations.qty)', 'order_id', 'product_id']

Upvotes: 1

Related Questions