0

In my User model I have fields follower_count and following_count.

And for followers and following I have HABTM association in my User as defined below:

public $hasAndBelongsToMany=array(
    'Following'=>array(
        'className'=>'User',
        'joinTable'=>'user_correlations',
        'foreignKey'=>'correlated_id',
        'associationForeignKey'=>'user_id',
        'fields'=>'id,first_name,last_name,photo_dir,photo',
        'order'=>'Rand()'
    ),
    'Followers'=>array(
        'className'=>'User',
        'joinTable'=>'user_correlations',
        'foreignKey'=>'user_id',
        'associationForeignKey'=>'correlated_id',
        'fields'=>'id,first_name,last_name,photo_dir,photo',
        'order'=>'Rand()'
    )
); 

How can I find Followers count and Following count in this case?

David Oliver
  • 2,424
  • 1
  • 24
  • 37
Meet Brahmbhatt
  • 57
  • 1
  • 1
  • 8

1 Answers1

0

Use the HABTM Counter Cache Plugin by Neil Crookes: https://github.com/neilcrookes/CakePHP-HABTM-Counter-Cache-Plugin

It definitely works for HABTM relationships between different models. I'm not sure if it will get confused by the fact that the Users model is on both sides of the relationship, but it's worth a try, and it'll be a good starting point for you even if it doesn't work out of the box.

joshua.paling
  • 13,762
  • 4
  • 45
  • 60
  • I tried to user that but not getting anything....not an single error so that i can move further in that direction,,,, Any way, I found my way without behavior...thank you for response joshua. – Meet Brahmbhatt Jan 12 '13 at 03:25