cakephp how to get the count in the query
Asked Answered
O

4

6

I am using this query to get all the data but I also need add in it how many rows it's returning.

Here is my code:

function getUsers($id){
    $users = $this->User->find('all', array('condition'=>array('my_id'=>$id)));
    $numUsers = sizeof($users);
    $users['User']['Total']= $numUsers;
    echo  json_encode($users);
}

The $dArray contains all the data for the users but I also want it to tell me how many users were there.

I was thinking that I would get data within the User object but this is what us gives me

Object
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
User: Object
Total: 6


instead of adding it to the 

0: Object
Company: Object
Group: Object
Team: Array[0]
User: Object
company_id: "20"
created: "2012-04-15 17:35:51"
email: "[email protected]"
expiration_date: "2012-04-15 17:35:00"
firstname: "jason"
grants_id: null
group_id: "1"
id: "1"
lastname: "brawar"
modified: "2012-04-23 14:25:48"
name: null
password: "203e686b6a6938648ddd3eb0a8d95988ac638596"
phone: "abcd"
status: "Active"
type: null
username: "asim"
Total: 1
Overreach answered 10/5, 2012 at 1:50 Comment(2)
The correct key name for the 'conditions' option is in plural, not singular.Calcar
Ask one question - Don't ask, then incorporate someone's answer into your newly revised, and completely different question.Root
R
9

No need to do it in the query - just use PHP:

<?php
$this->User->recursive=-1;
$users = $this->User->find('all', array('conditions'=>array('my_id'=>$id)));
$numUsers = sizeof($users);

This will give you the number of main items (ie Users), since any "extra" data you want to include via recursive or containable will be nested within each individual User array.

Root answered 10/5, 2012 at 1:54 Comment(7)
when I do this $users = $this->User->find('all', array('condition'=>array('my_id'=>$id))); $numUsers = sizeof($users); I get the followingOverreach
0: Object 1: Object 2: Object 3: Object 4: Object 5: Object User: Object Total: 6Overreach
let me edit my question with the latest code and result so you can see betterOverreach
You asked how to get the count, yet you're not even trying to show the count. Just echo $numUsers;Root
And just do this: $users['Total'] = $numUsers;Root
Very inefficient, you should do it on the query side, not on the PHP side. You don't want to hidrate the object with query data just to count the number of results. What if the count is extremely large?Albescent
@Albescent - as the OP mentioned, they're ALREADY getting all the data. Why would you run an additional query on the database to get the count of the things you already retrieved? If there's a problem with size of the data returned, that's a different issue and not really related to this question IMO.Root
B
12

Maybe you are looking for this...

$conditions = array('somecondition' => $someCond);
$totalUsers = $this->User->find('count', array('conditions' => $conditions));

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-count

Ok, now if you want to get the total number of elements that each user, should do the following:

$users = $this->User->find('all');
foreach($users as $key => $user)
$ElementsForEachUser[$key]['totalRows'] = sizeof($user['User']);
Boron answered 10/5, 2012 at 2:30 Comment(3)
OP: "...I also want it to tell me how many users were there."Root
what you mentioned above will return the total number of users, and if you put a condition, the function return the count filtering by that condition.Boron
I see where the confusion was. It was a big confusingly worded, but the OP said "but I also need add in it how many rows it's returning". So in this case, they're asking not for the total rows in the database, but the total of the rows returned.Root
R
9

No need to do it in the query - just use PHP:

<?php
$this->User->recursive=-1;
$users = $this->User->find('all', array('conditions'=>array('my_id'=>$id)));
$numUsers = sizeof($users);

This will give you the number of main items (ie Users), since any "extra" data you want to include via recursive or containable will be nested within each individual User array.

Root answered 10/5, 2012 at 1:54 Comment(7)
when I do this $users = $this->User->find('all', array('condition'=>array('my_id'=>$id))); $numUsers = sizeof($users); I get the followingOverreach
0: Object 1: Object 2: Object 3: Object 4: Object 5: Object User: Object Total: 6Overreach
let me edit my question with the latest code and result so you can see betterOverreach
You asked how to get the count, yet you're not even trying to show the count. Just echo $numUsers;Root
And just do this: $users['Total'] = $numUsers;Root
Very inefficient, you should do it on the query side, not on the PHP side. You don't want to hidrate the object with query data just to count the number of results. What if the count is extremely large?Albescent
@Albescent - as the OP mentioned, they're ALREADY getting all the data. Why would you run an additional query on the database to get the count of the things you already retrieved? If there's a problem with size of the data returned, that's a different issue and not really related to this question IMO.Root
E
2
$conditions = array('somecondition' => $someCond);
$totalUsers = $this->User->find('count', array('conditions' => $conditions));
Egis answered 4/4, 2013 at 9:56 Comment(0)
H
2
  1. If you already fetched the rows - sizeof($users);, like mentioned in other answers.

  2. However, if you want to execute sql's COUNT, since Cake 3 you can use ->count() on your query:

$query = $this->User->find('all');
$number = $query->count();
Hoey answered 7/4, 2020 at 1:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.