PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 27, 2022

[FIXED] Query builder fetch data in cakephp 3.2

 January 27, 2022     cakephp, cakephp-3.2, cakephp-3.x     No comments   

Issue

I am using this below code to sum up all my wallet balance .

$query = $this->Wallets->find();
$query->select([
    'count' => $query->func()->count('id'), 
    'total_price' => $query->func()->sum('amount')
])
->where(['status' => 4, 'user_id' => $user_id]);
pj($query);
echo $query->total_price;
exit;

out put of pj($query);

[
    {
        "count": 2,
        "total_price": 700
    }
]

Here i have tried to get single individual value using below query

echo $query->total_price;

I am not getting it. What is the proper syntax ,plz suggest me. Thanx.


Solution

$query = $this->Wallets->find();
$query->select([
    'count' => $query->func()->count('id'), 
    'total_price' => $query->func()->sum('amount')
])
->where(['status' => 4, 'user_id' => $user_id]);
debug($query);

// loop your results
foreach($query as $result){
   echo $result->total_price;
}

// or
$query->toArray();
echo $query[0]->total_price;
echo $query[1]->total_price;
...
exit;


Answered By - Salines
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing