Save various records in belongstomany association in cakephp 4.4

Alright… let me sum up whats happening here:

This is the DB schema you are talking about

As can be seen its not a “simple” belongsToMany association but an advanced one in that sence, that the junction table has an additional main_gate column

The data which is desired to be saved looks something like this:

but the entity which gets patched/created always has the main_gate set to B

The reason behind that is the fact, that a belongsToMany association expects you to only have 1 unique pair of foreign keys in your junction table - so there can’t be 2 rows like this present

sourcing_event_id | profile_id | main_gate
------------------------------------------
         1               1           A
         1               1           B

What you have here should be represented with normal hasMany associations between

  • profiles => sourcing_events_profiles
  • sourcing_events => sourcing_events_profiles

you already have an id column in your sourcing_events_profiles so it can be used like all the other tables and not just a “junction table” for a belongsToMany

To be clear: Saving additional data in the junction_table is fine and supported with the belongsToMany association. Your problem is just, that you want to save different addtional data for the same record

Thank you @KevinPfeifer, it worked perfect.