Deleting Records From Associated Table With PatchEntity in CakePHP

安稳与你 提交于 2019-12-07 07:53:28

I don't know if I completely get your question/wanted result, but from your line

Is there a way I can get the patch to delete records when there are fewer records in the request?

i read it as you want to replace the "old" associated data with the new associated data in a current post request.

A hasMany association's has 'saveStrategy' => 'append' as default (https://api.cakephp.org/3.4/source-class-Cake.ORM.Association.HasMany.html#84-89). (since version 3.1 i think)

You can change saveStrategy to replace like:

$this->hasMany('Employees', [
    'className' => 'Employees',
    'foreignKey' => 'employee_id',
    'saveStrategy' => 'replace'
]);

When saveStrategy is set to replace, only associated data in the current post/patchEntity will be associated to the current models record on save. Foreign key of existing linked associated data will be set to null.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!