How can I display a list in my controller ‘Contacts’ for a Many-To-Many table ?
user_id | company_id
1 1
1 2
What I need :
User First Name | User Last Name | Company Name | Company Postal Code
Darrell | Sparks | Car Progressive LTD | K8C 9V0
Darrell | Sparks | Collision Xpert | L1K 2X3
Do I need to :
add a PivotModel to the relationship (users belongs to many companies)
add a RelationController behavior / relation_config.yaml in my Contacts controller
display the relation instead of <?= $this->listRender() ?>
Not sure if that’s the easiest/smartest way to go.
I also thought to override listExtendRecords to purge the collection and create a new one from scratch (which is probably not the best)…
You will encounter an issue - the checkboxes won’t work. The table ‘author_plugin_users_companies’ don’t contain an ID field but a composite key. So if you try to click there, it won’t work as expected (there is no ID for each row). If you inspect the HTML, you will see there is no value for the differents inputs / labels.
The idea is to change the $record->getKey() with $record->your_first_key . ‘_’ . $record->your_second_key.
Now, if you refresh the list, you will be able to select the records you want because they have unique IDs and more importantly, be able to export (and you will know which record).