Multiple belongsToMany


#1

Is there a (simple) way to have multiple belongsToMany associations ?

The goal is to tag everything.

So, I have a Tags table and would like it to be associated with Articles, Categories, Images etc.

There used to be a tagEverything (I don’t remember the name exactly) plugin but it was cake 2 only.


#2

You can add as many belongsToMany associations as you see fit. Or did you mean a single association that pulls in all related entities of any type?


#3

Yes, a single association.
I just want to be able to tag any content, whatever type it can be.


#4

Seems like creating an association that uses a custom finder might be the way to go? Never tried, so I can’t provide sample code; give it a shot and see how it works?

Another option would be to have multiple associations, and a virtual field with a custom _get function that combines them all?


#5

Another option could be to not treat the tags as entity itself, but use a TagBehavior and a TagTrait (for controllers) to simply use/include it into any model/controller…

This way, you could have a tags-column on any entity to store the tags and can provide common search/add/edit/delete functions to models and controllers with minimum effort.


#6

Thank you for the ideas.
It seems to be the best solution.
I’ll let you know.