hasMany相关模型数据更新/修改中的问题 [英] Issue in hasMany associated model data update/modify
问题描述
我有两个表,
类别有很多产品
id
名称
有效
产品属于类别
id
名称
category_id
有效
在编辑类别时,同时,我还将显示与该类别相关的产品,以便我可以更新/修改与该类别相关的产品。问题:-当我为该类别添加了更多正常工作的产品时,但是当我从该类别中删除了某些产品时,删除的产品不会从数据库中删除。所以我想知道CakePHP是否支持此功能。如果是,请帮助我找出问题所在。
以下是保存代码:-
$ 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屋!