I need help to create a belongsToMany relation between 2 tables. I need this relation to be permanent and the "state of the relation" to be stored in an additional field in the relation table.
Here goes some background. I'm buildind a website for a restaurant.
This restaurant has a few cards (Brasserie, Gastronomique...):
cartes
id | name | description
Every card has many sections (Starters, Salad, Meat, Fish...). Every section may happen in multiple cards.
cartes_rubriques
id | name | description | comment
Cards and Sections have a belongsToMany
relation thanks to a table
cartes_cartes_rubriques
id | carte_id | rubrique_id| order | description | comment | active (boolean)
I need this relation to be permanent. The relation may be temporarly removed as some sections may be seasonal but I need to keep the order, description and comment to be saved.
I've tried 'saveStrategy' => 'append' but this only adds new records to cartes_cartes_rubriques.
CartesTable.php
$this->belongsToMany('Rubriques', [
'foreignKey' => 'carte_id',
'targetForeignKey' => 'rubrique_id',
'through' => 'cartes_rubriques'
]);
CartesRubriquesTable.php
$this->belongsToMany('Cartes', [
'foreignKey' => 'rubrique_id',
'targetForeignKey' => 'carte_id',
'through' => 'cartes_rubriques'
]);
CartesCartesRubriquesTable.php
$this->belongsTo('Cartes', [
'foreignKey' => 'carte_id',
'joinType' => 'INNER'
]);
$this->belongsTo('CartesRubriques', [
'foreignKey' => 'cartes_rubrique_id',
'joinType' => 'INNER'
]);
My strategy would ideally be to save theses relations and toggle cartes_cartes_rubriques.active.
Does this seem to be a good / possible strategy?
How can I make the toggeling on cartes_cartes_rubriques.active?
Do you have a better option?
Thanks for your help. Antonio