# 模型基类之常用数据处理方法 [TOC] ## 模型中的showReturnCode ~~~ static public function showReturnCode($code = '', $data = [], $msg = ''){ return \app\base\controller\Base::showReturnCode($code, $data, $msg); } static public function showReturnCodeWithOutData($code = '', $msg = '') { return \app\base\controller\Base::showReturnCode($code, [], $msg); } ~~~ ## 模型中的editData 很老的代码的 懒得改了 凑合用吧 使用方法详见 https://www.kancloud.cn/mikkle/thinkphp5_study/334906 ~~~ /** * 根据有Id修改信息 无Id 新增信息 * #User: Mikkle * #Email:776329498@qq.com * #Date: * @param $data * @return false|int|string * @throws */ public function editData($data){ if (isset($data['id'])){ if (is_numeric($data['id']) && $data['id']>0){ $save = $this->allowField(true)->save($data,[ 'id' => $data['id']]); }else{ $save = $this->allowField(true)->save($data); } }else{ $save = $this->allowField(true)->save($data); } if ( $save == 0 || $save == false) { $res=[ 'code'=> 1009, 'msg' => '数据更新失败', ]; }else{ $res=[ 'code'=> 1001, 'msg' => '数据更新成功', ]; } return $res; } ~~~ ## 创建个性GUID ~~~ /** * 创建个性GUID * Power by Mikkle * QQ:776329498 * @param string $base_code * @return string */ public function create_uuid($base_code = '') { if (empty($base_code)) { $base_name = basename(str_replace('\\', '/', get_called_class()), '.php'); $uuid_list = ModelCode::$uuid_list; $base_code = isset($uuid_list[$base_name]) ? $uuid_list[$base_name] : 'QT'; } $uuid = $base_code . strtoupper(uniqid()) . $this->builderRand(6); return $uuid; } ~~~ >[info] ModelCode::$uuid_list;中定义了个性前缀 这个代码写的比较早了 ,其实 $base_name =$this->name 就能获取到 ![](https://box.kancloud.cn/f60da94f764f6fc4c3cb0a1e37339389_381x455.png) ## 创建随机数 ~~~ /** * 创建随机数 * Power by Mikkle * QQ:776329498 * @param int $num 随机数位数 * @return string */ public function builderRand($num=8){ return substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, $num); } ~~~ ## 通过查询条件获取单条数据对象 ~~~ /** * 通过查询条件获取单条数据对象 * Power: Mikkle * Email:776329498@qq.com * @param array $map 查询条件 * @param bool|true $field 字段 * @param array $append 追加已经定义获取器字段 * @param bool|true $status * @return $this|array|false|\PDOStatement|string|Model */ public function getInfoByMap($map=[],$field=true,$append=[],$status=true){ if($status&&!isset($map['status'])){ $map['status']=1; } $object = $this->where($map)->field($field)->find(); if(!empty($object)&&!empty($append)){ return $object->append($append); }else{ return $object; } } ~~~ ## 通过查询条件获取单条数据(数组) ~~~ /** * 通过查询条件获取单条数据(数组) * User: Mikkle * Q Q:776329498 * @param array $map * @param bool|true $field * @param array $append * @param bool|true $status * @return array */ public function getArrayByMap($map=[],$field=true,$append=[],$status=true){ if($status&&!isset($map['status'])){ $map['status']=1; } $object = $this->where($map)->field($field)->find(); if(!empty($object)&&!empty($append)){ $return = $object->append($append); }else{ $return = $object; } return empty($return) ? [] : $return->toArray(); } ~~~ ## 通过查询条件获取多条数据(数组) ~~~ /** * Power: Mikkle * Email:776329498@qq.com * @param array $map * @param bool|true $field * @param array $append 这需要在模型里增加获取器 * @param bool|true $status * @return array */ public function getListByMap($map=[],$field=true,$append=[],$status=true){ if($status&&!isset($map['status'])){ $map['status']=1; } $object_list = $this->where($map)->field($field)->select(); $list=[]; if(!empty($object_list)){ foreach($object_list as $item=>$value){ if(!empty($append)){ $list[]= $value->append($append)->toArray(); }else{ $list[]= $value->toArray(); } } } return $list; } ~~~ ## 有乐观锁的快速修改 使用方法 https://www.kancloud.cn/mikkle/thinkphp5_study/359349 ~~~ /** * 带有乐观锁的修改 * Power: Mikkle * Email:776329498@qq.com * @param $save_data   修改的数据 * @param string $edit_pk 修改的ID字段名称 * @param string $version_field  乐观锁版本号字段名称 * @return array */ public function editDateWithLock($save_data,$edit_pk="",$version_field=""){ if (empty($version_field)){ $version_field = isset($this->versionField) ? $this->versionField : "edit_version"; } if (empty($edit_pk)){ $edit_pk = isset($this->editPk) ? $this->editPk : $this->getPk(); } //判断PK字段是否存在 if (!isset($save_data[$edit_pk])||!isset($save_data[$version_field])){ return self::showReturnCodeWithOutData(1003,"参数缺失"); }else{ //设置升级检索条件 PK和版本号 $map[$edit_pk] = $save_data[$edit_pk]; $map[$version_field] = $save_data[$version_field]; //剔除PK unset($save_data[$edit_pk]); } try{ //检测版本字段 if($this->hasColumn($version_field)){ throw new Exception("乐观锁版本字段[$version_field]不存在"); } $original_data = $this->where($map)->find(); if (empty($original_data)){ throw new Exception("此条信息已经变动了,请重新操作!"); } foreach ($save_data as $item=>$value){ if (isset($original_data[$item])){ //修改的数值不变时候 剔除 if ($original_data[$item]==$value){ unset( $save_data[$item]); }elseif($item!=$version_field){ unset( $original_data[$item]); } }else{ //修改的字段不存在 剔除 unset( $save_data[$item]); } } if(empty($save_data)){ throw new Exception("修改的数值无变化"); } //版本号升级 $save_data[$version_field]=(int)$original_data[$version_field]+1; if (1!=$this->allowField(true)->save($save_data,$map)){ throw new Exception("修改信息出错:".$this->getError()); } //记录修改日志 $this->saveEditLog($original_data,$save_data); return self::showReturnCodeWithOutData(1001); }catch (Exception $e){ $msg=$e->getMessage(); return self::showReturnCodeWithOutData(1003,$msg); } } ~~~ ## 判断字段是否存在 ~~~ /** * 判断字段是否存在 * Power: Mikkle * Email:776329498@qq.com * @param $column * @param string $table * @return bool */ protected function hasColumn($column,$table=""){ $table = isset($table)?$table:$this->table; if (empty($table)||$column){ $this->error="hasColumn方法参数缺失"; return false; } $sql = "SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name = '{$table}' AND column_name = '{$column}'"; return $this->query($sql) ? true : false; } ~~~