我有以下模型结构。
发表
id,title,text等。
代码
id,name,posts_count,created
tags_posts
id,tag_id,post_id,created
标签型号:
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' => ''
)
);
为什么TagsPost模型的belongsTo中的counterCache不起作用?我想在Tag模型中增加posts_count,如果发布了带有相关标签的帖子,如果删除了帖子,我想减少。
答案 0 :(得分:0)
要更新标记模型中的计数器缓存,需要更新标记模型,如
public $belongsTo = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id',
'conditions' => '',
'fields' => '',
'order' => '',
'counterCache' => 'posts_count'
)
);