使用model 保存数据失败

浏览:1057 发布日期:2018/05/11 分类:ThinkPHP5专区 关键字: tp5 bug
使用这种方法调用model,无法正确保存数据。
可能是比较错误,应该算是一个bug吧。

已经在model中定义了该字段的type,类型为string

0 与 00000000 成功
1 与 00000001 失败
00 与 0000000 失败
01 与 0000001 失败
0a 与 000000a 成功

调用方法如下// 获取目标数据
$AdminTeamModel = \app\common\model\AdminTeamModel::get($data['id']);
if(false === $AdminTeamModel){
    return $this->disposeErrorJson(1, '获取目标数据失败');
}else if(null === $AdminTeamModel){
    return $this->disposeErrorJson(1, '目标未找到');
}else if(1 === $AdminTeamModel->is_del){
    return $this->disposeErrorJson(1, '目标状态不正确');
};
// 执行操作
$AdminTeamModel->team_name = $data['team_name'];
$AdminTeamModel->team_remark = $data['team_remark'];
$result = $AdminTeamModel->save();
解决方法:
think\Model 中的 getChangedData 方法 修改!=为!==    /**
     * 获取变化的数据 并排除只读数据
     * @access public
     * @return array
     */
    public function getChangedData()
    {
        if ($this->force) {
            $data = $this->data;
        } else {
            $data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) {
                if ((empty($a) || empty($b)) && $a !== $b) {
                    return 1;
                }
                return is_object($a) || $a != $b ? 1 : 0;
            });
        }

        if (!empty($this->readonly)) {
            // 只读字段不允许更新
            foreach ($this->readonly as $key => $field) {
                if (isset($data[$field])) {
                    unset($data[$field]);
                }
            }
        }

        return $data;
    }
最佳答案
评论( 相关
后面还有条评论,点击查看>>