我使用cakephp 2.5.4并且想要在数据库中更新记录时,空字段,不要更改
这是我的更新功能
...
if ($this->User->save($this->request->data)) {
return $this->User->id;
}
...
答案 0 :(得分:5)
在保存数据之前,请从阵列中删除空值。
foreach ($this->request->data as $key => $value) {
if (empty($value))
unset($this->request->data[$key]);
}
if ($this->User->save($this->request->data)) {
return $this->User->id;
}
答案 1 :(得分:0)
添加条件 -
$conditions = array(
'conditions' => array('my_field IS NOT NULL')
);
$this->User->updateAll($this->request->data, $conditions);
并且您不需要$this->User->getLastInsertId();
,您将通过$this->User->id;