How to use the find with sum method?

Asked

Viewed 130 times

0

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

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 me the field valor_pago:

    $baixas = self::find()
    ->select('data_pagamento, SUM(valor_pago)')
    ->where(['=', "MONTH(data_pagamento)", date('m')])
    ->groupBy('DAY(data_pagamento)')
    ->all();

2 answers

2

Can also be obtained by

$this->find()->where(...)->sum('column');

1


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

Try to do so:

$baixas = self::find()
    ->select('data_pagamento, SUM(valor_pago) AS soma_valor')
    ->where(['=', "MONTH(data_pagamento)", date('m')])
    ->groupBy('DAY(data_pagamento)')
    ->all();
  • Is it serious that solved? It was a very well given kick.

  • Seriously, then I went to see that he makes the association of the query alias to the property of the object being accessed.

  • @Marcelodeandrade, then it was really the question that happens in Laravel. Exactly the same thing.

  • The same thing happened right after I needed to give one SELECT DAY(data_pagamento) AS data_pagamento. It automatically already makes the value associations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.