I have a situation where, for external reasons, I have to directly save join records rather than saving them as part of a join. Here's what I mean:
Firm
model whose data is pulled from an external source.County
model in my app databasecounties_firms
join table that I use to associate those external firms to counties.Because of what lives where, I'm not editing a Firm
model nor am I editing a County
model. I'm really just editing the associations. I have a Firm
model to encapsulate anything I need to do with Firm data and one of these methods is Firm::saveCounties( $data )
. It:
What I'm finding is that only the last county record is saved. Here's the incoming data:
Array
(
[0] => Array
(
[firm_id] => 13
[county_id] => 4fa16e24-a25c-4523-8a9e-7d1d147402e8
)
[1] => Array
(
[firm_id] => 13
[county_id] => 4fa16e27-ccd0-4f22-97da-7d1d147402e8
)
[2] => Array
(
[firm_id] => 13
[county_id] => 4fa16e4a-68f8-4fb1-95bb-7d1d147402e8
)
)
Given that data, I'm creating an on-the-fly association between Firm
and CountiesFirm
and attempting to $this->CountiesFirm->saveAll( $data )
.
As I mentioned, only the last of the 3 county associations in this example is getting saved. Any idea what I might be missing?
Thanks.
Your array is fine. It seems the model is not clearing its id, you can try and add ['id'] => null to every record in your array to force model id clearing.
This worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With