dongyu1983 2019-08-13 11:16
浏览 11

zend update正在删除旧值

I am doing update in zend which in some cases doesn't update all the fields, the fields that are not updated become null as if we are doing an add.

This is the code from the Controller

$result = $theuserModel->updateUserTest(
                       $id,
                       $this->getRequest()->getPost('user_name'),
                       /*some code*/
                       $this->getRequest()->getPost('user_postee')
               );
if ($result) {
   $this->view->notif = "Successfull Update";
   return $this->_forward('index');
}

The corresponding model

public function updateUserRest($id, $nom,$poste)
{
    $data = array(
        'user_name' => $nom,
        'user_postee' => $poste
    );
    $result=$this->update($data, 'user_id = '. (int)$id);
    return  $result;
}

I do an update for user_name only I found that the old value of user_postee got deleted and replaced by the default value (initial value which we get at the time of creation) for example null. Thanks in advance!

I have done this changes (bad solution) If anyone has another one optimised

->Controller

                    if($this->getRequest()->getPost('user_name')){
                    $resultname=$userModel->updateUserName($id,$this- 
                    >getRequest()->getPost('user_name'));
                    }

                    if($this->getRequest()->getPost('user_postee')){
                    $resultpostee=$userModel->updateUserPoste($id,$this- 
                    >getRequest()->getPost('user_postee'));
                    }
                    if ($resultname|| $resultpostee){
                    $this->view->notif = "Mise à jour effectuée";
                    return $this->_forward('index');
                          }

-> Model

        public function updateUserName($id, $name)
{
    $data = array(
        'user_name' => $name
    );
    $result=$this->update($data, 'user_id = '. (int)$id);
   return  $result;
}           
        public function updateUserPostee($id, $postee)
{
    $data = array(
        'user_postee' => $poste
    );
    $result=$this->update($data, 'user_id = '. (int)$id);
   return  $result;
}      
  • 写回答

2条回答 默认 最新

  • dongwuwan5646 2019-08-13 13:01
    关注

    that is complete correct response of update in Zend Db Table. I believe your assumption is if the value of 'user_postee' is null then it should not be updated into the database, am I correct. The answer is they will update the new value of "NULL" into the database. To avoid it , what you should do is using fetchrow() to get the value of the line by id foreach user_name and user_postee check if the value of them matching the array value your fetched , if nothing changed or Null, then use the old value from array , if new value exist use new value insert into the array , finally use update to update the new array into database

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大