首页 > 解决方案 > zend update 正在删除旧值

问题描述

我在 zend 中进行更新,在某些情况下不会更新所有字段,未更新的字段变为空,就好像我们正在添加一样。

这是来自控制器的代码

$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');
}

对应型号

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

我只更新了 user_name 我发现 user_postee 的旧值被删除并替换为默认值(我们在创建时获得的初始值),例如 null。提前致谢!

我已经完成了这个更改(糟糕的解决方案)如果有人优化了另一个

->控制器

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

-> 型号

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

标签: phpzend-framework

解决方案


这是 Zend Db 表中更新的完整正确响应。我相信您的假设是,如果“user_postee”的值为空,那么它不应该更新到数据库中,对吗?答案是他们会将“NULL”的新值更新到数据库中。为避免这种情况,您应该做的是使用 fetchrow() 通过 id foreach user_name 和 user_postee 获取行的值,检查它们的值是否与您获取的数组值匹配,如果没有任何变化或 Null,则使用旧值从数组中,如果存在新值,则使用新值插入数组,最后使用更新将新数组更新到数据库中


推荐阅读