cakephp $ this-> Model-> save()不更新 [英] cakephp $this->Model->save() not updating
问题描述
这是我的请求 - >数据
Array
(
[list] => Array
(
[4] > null
[2] => null
[3] => null
[5] => 3
)
$ b [
这是jquery序列化列表中的返回值。键是行id,值是行parent_id。
所以我循环控制器中的数组:
foreach($ this-> request-> data ['list'] as $ key => $ value){
(!isset [$ value]))? $ orders [$ value] = 0:$ orders [$ value] ++;
$ data = array('id'=> $ key,'parent_id'=>(int)$ value,'display_order'=> $ orders [$ value]);
$ this-> Category-> save($ data);
}
我在循环中创建的$ data数组是正确的,
我已经尝试过各种方式来保存:使用set()方法,使用$ this-> Category-> id = $ key;而不是直接在数据数组中添加键。
任何想法为什么这不是保存?我确定它是简单的...
我认为u忘了保存方法中的蛋糕约定,你有将所有的字段值放在Model名为FirstCap的数组中,类似这样:
Array
[ModelName] => Array
(
[fieldname1] =>'value'
[fieldname2] =>'value'
)
)
否则你可以使用set为每个值,并且你忘记创建一行对于每一个插入你就这样尝试这样的东西:
foreach($ this-> request-> data ['list '] as $ key => $ value){
(!isset($ orders [$ value]))? $ orders [$ value] = 0:$ orders [$ value] ++;
$ data = array('Category'=> array('id'=> $ key,'parent_id'=>(int)$ value,'display_order'=> $ orders [$ value] ));
$ this-> Category-> save($ data);
}
也可以设置每个id,然后迭代值
foreach($ this-> request-> data ['list'] as $ key => $ value){
(!isset($ orders [$ value]))? $ orders [$ value] = 0:$ orders [$ value] ++;
$ this-> Category-> id = $ key;
$ this-> Category-> set(array('parent_id'=>(int)$ value,
'display_order'=> $ orders [$ value]
) );
$ this-> Category-> save();
}
尝试每一个答案,但我认为最后一个会更适合你问题。
I'm trying to build an array and update a few fields in a loop.
This is my request->data
Array
(
[list] => Array
(
[4] => null
[2] => null
[3] => null
[5] => 3
)
)
It's the return value from a jquery serialized list. The key being the row id and the value being the rows parent_id.
So I loop through the array in the controller:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]);
$this->Category->save($data);
}
The $data array that I create in the loop is correct but the sql logs only shows SELECT COUNT(*) etc. for each row and no UPDATE commands.
I have tried all manner of ways to save this: using set() method, using $this->Category->id = $key; instead of directly adding the key in the data array.
Any ideas why this is not saving? I'm sure it is something simple...
i think u forgot the cake convention in the save method, you have to put all the field values inside an array with the Model name FirstCap too, something like this:
Array
(
[ModelName] => Array
(
[fieldname1] => 'value'
[fieldname2] => 'value'
)
)
Else you can use set for each and every value, and also youre forgetting to create one row for every insert youre making so try something like this:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array( 'Category' => array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]));
$this->Category->save($data);
}
Also you can set each and every id and then iterates over the values
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$this->Category->id = $key;
$this->Category->set(array('parent_id' => (int)$value,
'display_order' => $orders[$value]
));
$this->Category->save();
}
try each and every answer but i think the last one will fit better at your problem.
这篇关于cakephp $ this-> Model-> save()不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!