Relation Many to Many with the same model


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

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

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

        // 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 and you’ll find a complete list of configuration keys and their use.

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 !

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’