2

I have a BelongsToMany association, my tables are PostsTable, TagsTable and PostsTagsTable.

As explained here in the CakePHP book (associations), I have this fields:

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

Everything works correctly for now. So, as you can understand, now I want to use the tags.post_count field with CounterCache.

I followed the CakePHP book, but I suppose this is a special case, in fact it doesn't work by simply adding the behavior to the PostsTable.

Can you help me? Thanks.

Oops D'oh
  • 941
  • 1
  • 15
  • 34
Mirko Pagliai
  • 1,220
  • 1
  • 19
  • 36

1 Answers1

2

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.

UPDATE

Salines
  • 5,674
  • 3
  • 25
  • 50