How to use the find method with sum?

0

Using Yii2, I want to get the result of the following query:

SELECT data_pagamento, sum(valor_pago) 
FROM tbl_finlegado_titulo_parcela_baixa 
WHERE MONTH(data_pagamento) = 10 
GROUP BY DAY(data_pagamento)

Using the find method, it does not return the field valor_pago :

    $baixas = self::find()
    ->select('data_pagamento, SUM(valor_pago)')
    ->where(['=', "MONTH(data_pagamento)", date('m')])
    ->groupBy('DAY(data_pagamento)')
    ->all();
    
asked by anonymous 19.10.2015 / 14:56

2 answers

1

If field values are returned at the index of an array or object, then you should probably use alias to SUM(valor_pago) . So you will be able to access the returned values of the query correctly - I mention this because I use Laravel, and it has this problem.

Try to do this:

$baixas = self::find()
    ->select('data_pagamento, SUM(valor_pago) AS soma_valor')
    ->where(['=', "MONTH(data_pagamento)", date('m')])
    ->groupBy('DAY(data_pagamento)')
    ->all();
    
19.10.2015 / 15:25
2

Can also be obtained by

$this->find()->where(...)->sum('column');
    
25.05.2016 / 18:34