CakePHP 3 : CounterCache with BelongsToMany

标签 cakephp has-and-belongs-to-many counter-cache cakephp-3.1

我有一个 BelongsToMany 关联,我的表是 PostsTableTagsTablePostsTagsTable

如上所述here in the CakePHP book (associations) ,我有这个字段:

tags.id, tags.tag, tags.post_count
posts_tags.id, posts_tags.tag_id, posts_tags.post_id

目前一切正常。 因此,正如您所理解的,现在我想将 tags.post_count 字段与 CounterCache 一起使用。

我关注了CakePHP book ,但我认为这是一种特殊情况,事实上,简单地将行为添加到 PostsTable 中是行不通的。

你能帮我吗?谢谢。

最佳答案

From CakePHP Book

The CounterCache behavior works for belongsTo associations only. For example for “Comments belongsTo Articles”, you need to add the CounterCache behavior to the CommentsTable in order to generate comment_count for Articles table. It is possible though to make this work for belongsToMany associations. You need to enable the CounterCache behavior in a custom through table configured in association options. See how to configure a custom join table Using the ‘through’ Option.

更新

关于CakePHP 3 : CounterCache with BelongsToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32948513/

相关文章:

ruby-on-rails - 如何在 HABTM 中使用接受嵌套属性?

ruby-on-rails - Rails 3 和 Rspec : counter cache column being updated to 2 when expected 1

php - Cakephp counterCache 多个 counterScope - 逻辑问题

cakephp - 重置 auth 组件加载的用户数据

php - 如何在 gedit(windows 版本)上添加文件扩展名

php - 是否有可靠的方法将 Zurb Foundation CSS 框架与 CakePHP 一起使用?

javascript - Sequelize : Create join table attribute with set<Association>

optimization - 如何优化基于 cakephp 和 SQL 的 Web 应用程序?

ruby-on-rails - counter_cache 在 after_create 钩子(Hook)中是陈旧的