Cakephp how to get invoice in request

I use this query to get all the data, but I also need to add the number of rows that it returns to it.

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);
}

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

I thought I was getting data in a User object, but this is what we give 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: "dd@gmail.com"
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
+3
source share
3 answers

No need to do this in the request - 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 basic elements (i.e. users), since any "additional" data that you want to include through recursive or pent-up data will be embedded in each individual user array.

+7
source

Perhaps 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

, , , , :

$users = $this->User->find('all');
foreach($users as $key => $user)
$ElementsForEachUser[$key]['totalRows'] = sizeof($user['User']);
+8
$conditions = array('somecondition' => $someCond);
$totalUsers = $this->User->find('count', array('conditions' => $conditions));
+2

All Articles