Possible Duplicate:
CakePHP Subscribe Users Model
I have three models
- User
- Post
- Friendship
What i want is that if USER A have subscribed to USER B and USER C, Then USER A may only be able to view posts shared by USER B and C and not others.
First, i want to know if i made proper associations.
User Model:
public $hasAndBelongsToMany = array(
'UserFriendship' => array(
'className' => 'User',
'joinTable' => 'friendships',
'foreignKey' => 'user_from',
'associationForeignKey' => 'user_to'
)
);
Friendship Model:
public $hasMany = array(
'FriendFrom'=>array(
'className'=>'Friendship',
'foreignKey'=>'user_from'
),
'FriendTo'=>array(
'className'=>'Friendship',
'foreignKey'=>'user_to'
)
);
public $belongsTo = array(
'UserFrom'=>array(
'className'=>'User',
'foreignKey'=>'user_from'
),
'UserTo'=>array(
'className'=>'User',
'foreignKey'=>'user_to'
)
);
If the associations are correct what exactly i have to do with Post Model or Post Controller or any where else to show USER A the posts of user`s he is subscribed to?
I just need a bit of hint as i am not able to make any appropriate logic.
I always find the easiest way to do this is: find the user ID’s who are user A’s friends:
After that, you can find all posts posted where the
Post.user_idisINyour array of User ID’s: