Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SUM query in cakephp 3 not working

I am trying to add the data of same field and want to return a result i used the following query:

$total = $this->Details->find('all', array(
    'fields' => array('sum(Details.total_downtime+ Details.total_downtime)'), 
    'conditions' => array('Details.site_id' => $id)
));    
print_r($total->toArray());
exit;

And I am getting the following result:

Array ( 
    [0] => App\Model\Entity\Detail Object ( 
        [displayField] => username 
        [_accessible:protected] => Array ( 
            [*] => 1 
            [id] => 1 
            [site_id] => 1 
            [uptime] => 1 
            [downtime] => 1 
        ) 
        [_properties:protected] => Array ( 
             [sum(Details] => Array ( [total_downtime+ Details] => 4 ) 
        ) 
        [_original:protected] => Array ( ) 
        [_hidden:protected] => Array ( ) 
        [_virtual:protected] => Array ( ) 
        [_className:protected] => App\Model\Entity\Detail [_dirty:protected] => Array ( ) 
        [_new:protected] => 
        [_errors:protected] => Array ( ) 
        [_registryAlias:protected] => Details 
    ) 
)

Where can I find my sum?

like image 249
Rishu Avatar asked May 22 '26 00:05

Rishu


1 Answers

I guess you are trying to achieve something like this?

$query = $Details->find(); 
$query
    ->select(['sum' => $query->func()->sum('Details.total_downtime')])
    ->where(['Details.site_id' => $id])
    ->toArray();
like image 136
makallio85 Avatar answered May 23 '26 15:05

makallio85



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!