I have a many-to-many relationship between Invoices and Items. An Item can appear multiple times in the same Invoice, but with different amounts (e.g. when the same service is used several times during one month).
I was hoping I could create this association by including the following in the 'items' element of the invoice when saving (see https://book.cakephp.org/3/en/orm/saving-data.html#saving-belongstomany-associations):
Array
(
[0] => Array
(
[id] => 1
[_joinData] => Array
(
[amount] => 5338.29
)
)
[1] => Array
(
[id] => 1
[_joinData] => Array
(
[amount] => 5988.53
)
)
[2] => Array
(
[id] => 1
[_joinData] => Array
(
[amount] => 6023.40
)
)
)
In the example above, the result I'm hoping for is that three rows are created in the join table invoices_items
. The invoice is saved correctly, but only one row is created in the join table.
One both associations I tried setting saveStrategy
to append
(I wasn't sure what this does), but this didn't help:
$this->belongsToMany('Invoices', [
'saveStrategy' => 'append'
]);
Is it possible to achieve this behaviour out of the box, or do I need to create something more custom, like a new model to specifically keep track of these relationships? Please let me know if more code would help to clarify what I'm trying to achieve.
Thanks!