'unique' => 'keepExisting' equivalent in CakePHP 3

Hi!
We have a problem with our migration from CakePHP 2.10 to 3. Some of our Models use HABTM relations and use the unique key with value: keepExisting

unique: boolean or string keepExisting.

If true (default value) CakePHP will first delete existing relationship records in the foreign keys table before inserting new ones. Existing associations need to be passed again when updating.

When false, CakePHP will insert the specified new relationship records and leave any existing relationship records in place, possibly resulting in duplicate relationship records.

When set to keepExisting, the behavior is similar to true, but with an additional check so that if any of the records to be added are duplicates of an existing relationship record, the existing relationship record is not deleted, and the duplicate is ignored. This can be useful if, for example, the join table has additional data in it that needs to be retained.

https://book.cakephp.org/3/en/orm/associations.html#belongstomany-associations

What is the CakePHP 3 equivalent of this key? I read something about through, and the dependent key seems like it could help but I can’t quite combine these two in my head completely. What do I need to do to associate 2 Models with each other and not delete the connection entries in my join table but let CakePHP ignore them.

  • dependent : When the dependent key is set to false , and an entity is deleted, the data of the join table will not be deleted.
  • through : Allows you to provide either the alias of the Table instance you want used on the join table, or the instance itself. This makes customizing the join table keys possible, and allows you to customize the behavior of the pivot table.

https://book.cakephp.org/3/en/orm/associations.html#default-association-conditions

saveStrategy : Either ‘append’ or ‘replace’. Defaults to ‘replace’. Indicates the mode to be used for saving associated entities. The former will only create new links between both side of the relation and the latter will do a wipe and replace to create the links between the passed entities when saving.

1 Like

Ah thank you so much!