tags:

views:

38

answers:

3

Hi,

I have a built a cakephp application. I have two models

one is groups and other is users

groups hasMany users users belongsTo Groups

in my users table I have group_id column which displays the group id to which the user belongsto. (user can only belongto one group)

Now if I want to display count of all users in particular groups..

like sports(19) where sports is the group name and 19 is the number of users in that group.

how could I get the output as sports(19)?

I appreciate any help.

Thanks.

+1  A: 

I would advice you to study the cookbook on Retrieving Your Data at first.

bancer
Actually I did, But i was wondering because when i retrieve data from user's table, i get group_id from the user's table. I was wondering how could i get the group name?
Nick
It would be good to provide us some code you have. F.ex. your find() call.
bancer
A: 

If it hasMany Post-s, add this to the options array:

'contains' => 'Post'

See containable in the Cookbook.

sibidiba
+1  A: 

On your first question linking and fetching results from has many:

basically if you have proper links in your Users model, then Groups will be fetched automatically in following format:

[User]=>array(
   [0]=>array(
      [id]=>1,
      [name]=>'John',
      [group_id]=>1,
      [Group]=>array(
          [id]=>1,
          [name]=>'All'

About counting of the groups. You can always count the Groups under each used by counting nodes of the array, but the smarter way is to use counterCache.

Nik