I'd like to get the value of select sum() in CakePHP3.

Asked 1 years ago, Updated 1 years ago, 97 views

Hi, everyone.When I make a household account book with CakePHP3, I would like to be able to give the total price of what I bought.

I wrote the select sum value while looking at the official documentation, but the SQL issued itself appears instead of the result.
I wonder why...

// Householdscontroller.php
public function index()
{
    // db made and baked
    $this->paginate=[
        US>'contain'=>['Tags']
    ];
    $this->set('households', $this->paginate($this->households)));
    $this->set('_serialize', ['households']);

    // Processing to issue a total amount
    $query=$this->Householdholds->find();
    $amount=$query
            ->where (['status'=>'out'])
            ->select(['sum'=>$query->func()->sum('payment')]);
    $this->set('amount',$amount);
}

If you look at the $amount part in the view,

SELECT(SUM(payment)) FROM households households WHERE status=:c0

It says.I want numbers, not SQL.

By the way, I'm not sure why there are two things below
When I tried phpMyAdmin as it was, the value was successfully obtained.

cakephp

2022-09-30 21:12

1 Answers

Try $amount->first() or $amount->all().
Then you can get the entity, so I think you can get the total value by $entity->sum.
If you want to take an associative array, you can take it with ->toArray().


2022-09-30 21:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.