0

我有以下模型结构。

邮政

id、标题、文本等

标签

id,名称,posts_count,创建

标签帖子

id、tag_id、post_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' => ''
        )
);

标签邮政模型:

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 不起作用?如果发布了带有相关标签的帖子,我想增加标签模型中的posts_count,如果帖子被删除,我想减少。

4

1 回答 1

0

要更新标签模型中的计数器缓存,需要更新标签模型,如

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

)

);

于 2014-12-24T05:25:58.013 回答