# Multiple belongsToMany

**URL:** <https://discourse.cakephp.org/t/multiple-belongstomany/5698>\
**Category:** Need Help\
**Created:** [February 23, 2019, 2:36pm UTC](https://discourse.cakephp.org/t/multiple-belongstomany/5698 "2019-02-23T14:36:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [February 23, 2019, 2:36pm UTC](https://discourse.cakephp.org/t/multiple-belongstomany/5698/1 "2019-02-23T14:36:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 23, 2019, 5:59pm UTC](https://discourse.cakephp.org/t/multiple-belongstomany/5698/2 "2019-02-23T17:59:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [February 24, 2019, 10:19pm UTC](https://discourse.cakephp.org/t/multiple-belongstomany/5698/3 "2019-02-24T22:19:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 25, 2019, 1:08am UTC](https://discourse.cakephp.org/t/multiple-belongstomany/5698/4 "2019-02-25T01:08:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dmuenstermann](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmuenstermann](https://discourse.cakephp.org/u/dmuenstermann)\
**Post date:** [March 5, 2019, 10:47am UTC](https://discourse.cakephp.org/t/multiple-belongstomany/5698/5 "2019-03-05T10:47:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [March 7, 2019, 10:22am UTC](https://discourse.cakephp.org/t/multiple-belongstomany/5698/6 "2019-03-07T10:22:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![calzone](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/calzone/32/305_2.png) [@calzone](https://discourse.cakephp.org/u/calzone)\
**Post date:** [February 15, 2021, 6:54am UTC](https://discourse.cakephp.org/t/multiple-belongstomany/5698/7 "2021-02-15T06:54:30Z")

</div>

Apologies about reviving an old thread. Just ignore.

I chose to do so, however, because the solution to this question is now documented in the other thread I recently created, I figured as an aid anyone doing searches on how to do this, I should post a link to the other thread here:

> [@Many to Many where one table can be any table?](https://discourse.cakephp.org/t/many-to-many-where-one-table-can-be-any-table/8992):
>
> I have a tags table: id, name And I have an entity\_tags table: id, tag\_id, tab\_name, f\_key The idea was that I could create a tag in the tags table and then assign it to any record in more than a single table in my database. So entity\_tags would have the id of the tag, as well as the name of the table and id of the record that the tag was attached to. Then I started putting together my cake model and realized I’m not sure this is actually remotely supported. If anyone has done this before or…
