# Relation Many to Many with the same model

**URL:** <https://discourse.cakephp.org/t/relation-many-to-many-with-the-same-model/7701>\
**Category:** Uncategorized\
**Created:** [April 22, 2020, 5:02am UTC](https://discourse.cakephp.org/t/relation-many-to-many-with-the-same-model/7701 "2020-04-22T05:02:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![froggyp](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@froggyp](https://discourse.cakephp.org/u/froggyp)\
**Post date:** [April 22, 2020, 5:02am UTC](https://discourse.cakephp.org/t/relation-many-to-many-with-the-same-model/7701/1 "2020-04-22T05:02:15Z")

</div>

Hello,

I need to connect components with links by relationship **Many to Many**.  
Therefore I have a components table and a pivot table named components\_components.

A component is linked to another one. Many kind of links can exist between two components.  
**Component From ====\> Component To**

The db tables are like that

 ![Component_scheme](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/2/254637220fce468c4334a2a204551f9569e7890b.jpeg)

I understand that I need to have a specific joinModel because conventions are not ok, but i don’t understand well how to set parameter for relationships in both tables.

Can you help me to find out the good way to use cakephp in this case ?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [April 22, 2020, 3:50pm UTC](https://discourse.cakephp.org/t/relation-many-to-many-with-the-same-model/7701/2 "2020-04-22T15:50:24Z")

</div>

Can’t guarantee success, but I believe this would work:

```auto
        // A component sees its parents here
        $this->belongsToMany('ParentComponent', [
            'className' => 'Components',
            'foreignKey' => 'component_from_id',
            'targetForeignKey' => 'component_to_id'
        ]);
        // A component sees its children here
        $this->belongsToMany('ChildComponent', [
            'className' => 'Components',
            'foreignKey' => 'component_to_id',
            'targetForeignKey' => 'component_from_id'
        ]);

```

Scroll down a little [from here](https://book.cakephp.org/4/en/orm/associations.html#belongstomany-associations) and you’ll find a complete list of configuration keys and their use.

---

<div class="post-metadata">

**Author:** ![froggyp](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@froggyp](https://discourse.cakephp.org/u/froggyp)\
**Post date:** [April 24, 2020, 5:59am UTC](https://discourse.cakephp.org/t/relation-many-to-many-with-the-same-model/7701/3 "2020-04-24T05:59:00Z")

</div>

Thanks for your help dreamingmind. It was helpful.

I started from here and I found another one useful information. I post it because this could help later !

> <https://github.com/cakephp/cakephp/issues/13410>
>
> This is a (multiple allowed):
> 
> bug
> 
> enhancement
> 
> feature-discussion (RFC)
> 
> CakePHP Version: 3.8.0
> 
> Gist with files: https://gist.github.com/andres-ml/0723ffab34454c20d232cb9544ef9b12
> What you did
> Define two associations using the same...

Define two associations using the same table (users\_parents):  
Users belongsToMany Parents  
Users belongsToMany Children

Specifications in UsersTable.php

$this-\>belongsToMany(‘Parents’, [  
‘className’ =\> ‘Users’,  
‘foreignKey’ =\> ‘child\_user\_id’,  
‘targetForeignKey’ =\> ‘parent\_user\_id’,  
‘through’ =\> ‘UsersParents’  
]);

$this-\>belongsToMany(‘Children’, [  
‘className’ =\> ‘Users’,  
‘foreignKey’ =\> ‘parent\_user\_id’,  
‘targetForeignKey’ =\> ‘child\_user\_id’,  
‘through’ =\> ‘UsersParents’  
]);
