Explaining saveStrategy for hasMany-Association

Could somebody explain to me with an example what save strategy “append” or “replace” means in a hasMany-Association?
I’ m afraid the explanation given in https://book.cakephp.org/3.0/en/orm/associations.html is not quite clear to me.
What would it mean for the tables defined in the cakephp-tutorial?

Append means that pre-existing relations to the entity in question will be retained, with new ones added. Replace means they’ll be deleted and only the new ones will be kept.

1 Like

Suppose you have an users-table and an articles-table and an user can have many articles as in the tutorial.
If i add or edit an article i can’t see any difference between setting saveStrategy to “replace” or “append” in the users table and then saving an article.

If i change saveStrategy for the tags-association from append to replace in the cake-php tutorial i can see the difference.
But, i am afraid, if i do so for the hasMany-association between the users-table and articles-table in the tutorial i cannot see that any entry will be deleted in the database.
Does somebody know where my mistake is?

I don’t think that the strategy for articles that you specify in the users table comes into play at all when you are adding or editing an article. It would be used if you had some way of selecting the articles to associate with the user, on the user edit page. But that particular example likely doesn’t make much sense.

Thank you for your answer.
As you write in your answer that using the option saveStrategy doesn’t come into play and doesn’t make sense in my example i ask myself when using it in hasMany-associations makes sense in general.
Can somebody give an real example for using the saveStrategy-option in hasMany-associations in a right way that makes more sense than my example?

Person has many phone numbers, when person update the phone number
with append strategy:
maintain all associated saved phones from database and add/update any new number present in the form

with replace strategy
remove all associated saved phones from database and maintain ONLY the ones present in the form

1 Like