php - 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;
}
解决方案
这是 Zend Db 表中更新的完整正确响应。我相信您的假设是,如果“user_postee”的值为空,那么它不应该更新到数据库中,对吗?答案是他们会将“NULL”的新值更新到数据库中。为避免这种情况,您应该做的是使用 fetchrow() 通过 id foreach user_name 和 user_postee 获取行的值,检查它们的值是否与您获取的数组值匹配,如果没有任何变化或 Null,则使用旧值从数组中,如果存在新值,则使用新值插入数组,最后使用更新将新数组更新到数据库中
推荐阅读
- angular - 角度相同的应用程序中的多个域
- javascript - 如何通过 Javascript 设置在 1 次点击时出现的表格
- python - 尝试绘制角色时Pygame蛇游戏属性错误
- cinema-4d - 纹理编辑器的网格不显示
- vue.js - 我们可以在不调用“this”的情况下使用库函数吗?
- go - 从 interface{} 到嵌入类型的类型断言
- julia - 如何使用 julia 的 PackageCompiler 搭建 PlotStats 的快速启动环境?
- shell - 防止 sigint 在将 OpenOCD 与 GDB 一起使用时关闭 OpenOCD
- ssl - 在 Windows VPS 中将 SSL 证书添加到 Net Core 应用程序
- javascript - 使用 jQuery 将 HTML 加载到另一个 HTML 的 div 上