Reputation: 4084
I am using Cakephp 2.0 My model relationship as follows:
User has many Post
Post has many Comment
Post has many Like
Comment has many Like
In Post table I added a column "like_count" and enable the countercache and In Comment table I added a column "like_count" and enable the countercache
Likes table contain:
id,post_id,comment_idu,user_id
and Model as follows :
public $belongsTo = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id',
'conditions' => '',
'fields' => '',
'order' => '',
'counterCache' => true
),
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'comment_id',
'conditions' => '',
'fields' => '',
'order' => '',
'counterCache' => true
))
Its working Fine..
But Now I need to do total like count(Post like count + Comment like count) for each user. so I planned to add a field in users table and enable the countercache But totally strucked.. what will be the column name i need to add in users table and where i need to put Countercache => true. I dont how to go further..
Upvotes: 2
Views: 1041
Reputation: 651
Add the following to your $belongsTo array.
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => '',
'counterCache' => true
),
And you'll add a like_count
column in the users
table.
And in your UserModel
you'll need to add a HasMany for likes of course.
Upvotes: 1