hasMany相关模型数据更新/修改中的问题 [英] Issue in hasMany associated model data update/modify

查看:100
本文介绍了hasMany相关模型数据更新/修改中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


 我有两个表,
类别有很多产品
id
名称
有效

产品属于类别
id
名称
category_id
有效


在编辑类别时,同时,我还将显示与该类别相关的产品,以便我可以更新/修改与该类别相关的产品。

以下是保存代码:-

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

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


this->类别-> save($ entity);

}


解决方案

当您设置hasMany关系,添加'saveStrategy'=> ‘替换’。有关详细信息,请参见手册的有很多部分


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);

}

解决方案

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

这篇关于hasMany相关模型数据更新/修改中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆