2014-12-23 80 views
0

我有以下模型結構。屬於的CakePHP counterCache(HABTM)

ID,標題,文字等

標籤

ID,姓名,POSTS_COUNT,創建

tags_posts

ID, tag_id,pos T_ID,創建

標籤型號:

public $hasAndBelongsToMany = array(
     'Post' => 
     array(
       'className' => 'Post, 
       'joinTable' => 'tags_posts', 
       'foreignKey' => 'tag_id', 
       'associationForeignKey' => 'post_id', 
       'unique' => true, 
       'conditions' => '', 
       'fields' => '', 
       'order' => '', 
       'limit' => '', 
       'offset' => '', 
       'finderQuery' => '' 
     ) 
); 

郵政型號:

public $hasAndBelongsToMany = array(
     'Tag' => 
     array(
       'className' => 'Tag', 
       'joinTable' => 'tags_posts', 
       'foreignKey' => 'post_id', 
       'associationForeignKey' => 'tag_id', 
       'unique' => true, 
       'conditions' => '', 
       'fields' => '', 
       'order' => '', 
       'limit' => '', 
       'offset' => '', 
       'finderQuery' => '' 
     ) 
); 

TagsPost型號:

public $belongsTo = array(
    'Tag' => array(
     'className' => 'Tag', 
     'foreignKey' => 'tag_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'counterCache' => 'posts_count' 

    ), 
    'Post' => array(
     'className' => 'Post', 
     'foreignKey' => 'post_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 
); 

爲什麼不工作counterCache在TagsPost型號的屬於關聯?我想增加標記模型中的posts_count,如果發佈了包含相關代碼的帖子,並且在刪除帖子時減少。

回答

0

要在標籤模型更新計數器緩存,需要更新等爲

public $belongsTo = array(
'Post' => array(
    'className' => 'Post', 
    'foreignKey' => 'post_id', 
    'conditions' => '', 
    'fields' => '', 
    'order' => '', 
    'counterCache' => 'posts_count' 

) 

標籤模型);

+0

但我會計算在帖子中使用的標籤而不是書面帖子的數量。因爲我需要這個「最受歡迎的標籤」 – user3005837