2012-04-13 90 views
2

我使用CakePHP 2.0我的模型關係如下:CakePHP的Countercache - 邏輯問題

用戶有很多帖子

張貼有許多評論

郵政有很多喜歡

評論有很多像

在帖子表格我添加一列「LIKE_COUNT」並啓用countercache 和 在評語表,我添加一列「LIKE_COUNT」並啓用countercache

喜歡錶包含:

ID,POST_ID,comment_idu邀請,USER_ID

和型號如下:

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 
    )) 

它的工作很好..

,但現在我需要做的總像數(後像數+評論像 數)爲每個用戶。所以我計劃在用戶表中添加一個字段,並且啓用countercache但是完全被攻擊..將會在列表 名稱中添加用戶表,並且我需要將Countercache添加到 => true。我不怎麼走得更遠..

回答

1

將以下內容添加到您的$ belongsTo數組中。

'User' => array(
     'className' => 'User', 
     'foreignKey' => 'user_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'counterCache' => true 
    ), 

你會在users表中添加like_count列。


而在你的UserModel,你需要添加一個HasMany當然喜歡。

+0

其作品..以及 – AnNaMaLaI 2012-04-13 13:53:05

+0

很高興幫助! – Joep 2012-04-13 14:15:01