CakePHP counterCache в ownTo (HABTM)

У меня есть следующая структура модели.

Сообщение

идентификатор, заголовок, текст и т. д.

Тег

id, name, posts_count, создан

tags_posts

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 не работает? Я хочу увеличить количество posts_count в модели тегов, если была опубликована запись с соответствующими тегами, и уменьшить, если сообщение удалено.

0

Решение

Чтобы обновить кеш счетчика в модели тегов, необходимо обновить модель тегов, например, как

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

)

);

0

Другие решения

Других решений пока нет …