在CakePHP 2.3项目中,在我想要更新表的几个记录的控制器操作之一中。要更新的数据作为数组发布,我遍历该数组。
但是,一些新的字段值与当前字段值有关,因此我不能简单地将数据写入数组$ data并进行模型保存($ data)。相反,我做
$record = model->read(null, $id); //$id is retrieved from the posted data array.
$record['some_field'] = $new_value;
unset($record['modified']);
//in addition I used model->modified = null;, but to no avail
model->save($record);
问题是, modified 字段未自动更新。在CakePHP文档中,我发现“Modified”的值不得出现在要保存的数据中。但是单独的unset()似乎还不够。
在cakePHP - modified field not updating用户tadasZ中提到它在您提前使用model-> read()时不起作用。
我在文档中找不到任何相关内容。但如果是这种情况,是否有任何方法可以将
答案 0 :(得分:1)
当您使用Model::read()
时,结果仍然是$array['Model']['field']
的CakePHP格式,因此您必须执行unset($record['Model']['modified']);
答案 1 :(得分:0)
答案在这里: http://book.cakephp.org/2.0/en/models/saving-your-data.html#using-created-and-modified
如果您在Model :: save()之前在$ this->数据中创建或修改了数据(例如来自Model :: read或Model :: set),那么这些值将取自$ this- >数据而非自动更新。如果你不想要,你可以使用unset($ this-> data [' Model'] [' modified'])等。