How to implement backend with two level deep relations (belongsToMany)

Hello
I’m trying to develop my first plugin and I don’t know if I’m missing something, if I’ve implemented wrong database relations or if it’s a limitation of OctoberCMS.

I have these 3 tables

domain
±—±---------------±----------------±--------------------±--------------------+
| id | domain_name
±—±---------------±----------------±--------------------±--------------------+

domain_view

±---------±-------------±-------±--------±--------±----------±-----------±--------------------+
| position | active | view_id | domain_id
±---------±-------------±-------±--------±--------±----------±-----------±--------------------+

view

±—±-------------------±---------±--------------------±--------------------+
| id | name
±—±-------------------±---------±--------------------±--------------------+

Now these three tables make up the belongsToMany relationship where Domain is the main model, View the final model and DomainView is the pivot model.
With this configuration this part works fine with RelationController behaviour.

Domain.php

    public $belongsToMany = [
        'views' => [
            \Author\Plugin\Models\View::class,
            'table' => 'author_plugin_ domain_view',
            'pivot' => ['position' 'active']
        ]
        
    ];

config_relation.yaml (domain)

views: 
 ....................
  pivot:
    form:
      fields:
        pivot[position]:
          label: Position
          span: full
          type: text
        pivot[active]:
          label: Active
          span: left
          type: checkbox

So that in the backend under Domain controller I can add related Views and when I click of the View I can edit the data inside the pivot DomainView model.

However I also have these two tables

view_parameter;
±—±--------±--------+
| id | name | view_id |
±—±--------±--------+

domain_view_parameter;
±—±--------±--------+
| value | view_parameter_id | view_domain_id |
±—±--------±--------+

like for the first example this also involved three tables with a pivot table

DomainView.php

    public $belongsToMany = [
        'parameters' => [
            \Author\Plugin\Models\ViewParameter::class,
            'table'    => 'author_plugin_domain_view_parameter',
            'key'      => 'view_domain_id',
            'otherKey' => 'view_parameters_id'
        ]
    ];

config_relation.yaml (domainview)

parameters: 
  .....
  pivot:
    form:
      fields:
        name: 
          label: Name
        pivot[value]:
          label: Value
          type: text

At this point I don’t know how to proceed, in the Domain backend once I click on the DomainView pivot how can I link related ViewParameters and edit the DomainViewParameters pivot data?

Still trying to figure out if it’s possible to go in deeper relations…

Only manually: Tutorial 21: Beyond Behaviors - Part 3: Implementing a nested relationship - October CMS

1 Like

The RelationController is a basic implementation of List and Form widgets. If you have more intricate requirements, it is recommended to work with those widgets directly in your controller.