cakedon';更新时不能更改空字段


cakedon't change null field on update

我使用cakehp2.5.4,并希望在更新数据库中的记录时,字段为空,不更改

这是我的更新功能

...
   if ($this->User->save($this->request->data)) {
       return $this->User->id;
   }
...

在保存数据之前,请从数组中删除空值。

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

添加条件-

$conditions = array(
               'conditions' => array('my_field IS NOT NULL')
              );
$this->User->updateAll($this->request->data, $conditions);

并且您不需要$this->User->getLastInsertId();,您将通过$this->User->id;

获得