Issue in hasMany associated model data update/modify

北慕城南 提交于 2020-01-06 06:11:12

问题


I have two table,
categories     hasMany products
id
name
active

products       belongsTo categories
id
name
category_id
active

When I am editing the categories, in the same time I am also displaying the Products related to the category so that I can update/modify products related to the category.

Issue:- When I add more products for the category that works fine but when I remove some products from the category,the removed product does not get deleted from the database. So I want to know that This functionality is supported by CakePHP or not. If yes please help me to find where I am going wrong.

Here is the save code:-

$categoryProducts = $this->Categories->get(1, [
   'contain' => 'Products' 
]);

    if($this->request->is['post', 'put']){
       $entity = $this->Categories->patchEntity($categoryProducts, $this->request->data);


      $this->Categories->save($entity);

}

回答1:


When you set up your hasMany relationship, add 'saveStrategy' => 'replace'. See the hasMany section of the manual for details.



来源:https://stackoverflow.com/questions/47739248/issue-in-hasmany-associated-model-data-update-modify

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