【问题标题】:CakePHP 3: CounterCache with BelongsToManyCakePHP 3:带有 BelongsToMany 的 CounterCache
【发布时间】:2015-10-05 12:21:43
【问题描述】:

我有一个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中是行不通的。

你能帮帮我吗?谢谢。

【问题讨论】:

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


    【解决方案1】:

    From CakePHP Book

    CounterCache 行为仅适用于 belongsTo 关联。例如对于“Comments belongsTo Articles”,您需要将 CounterCache 行为添加到 CommentsTable 以便为 Articles 表生成 comment_count。 虽然可以使这项工作适用于 belongsToMany 关联。您需要在关联选项中配置的自定义直通表中启用 CounterCache 行为。了解如何使用“通过”选项配置自定义连接表。

    更新

    【讨论】:

    • 抱歉@Salines,'post_tags' 表上的post_count 字段?你确定吗?
    • 我把 cakephp 书中的内容写给你。但是你需要的是每个tag的posts总和,tag和posts之间的habtm关系在哪里?
    • 是的,当然,我想要每个标签的帖子计数
    猜你喜欢
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多