0
votes

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.

2

2 Answers

1
votes

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.

0
votes

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

'contains' => 'Post'

See containable in the Cookbook.