ba
其他模型继承ba
<?php
namespace app\common\model;
use think\Model;
use think\Validate;
/**
* Base
*/
class Base extends Model
{
//通过字段类型返回对应的验证规则
public static function _getTypeRule($type){
$rule = ["int"=>"number","tinyint"=>"number","smallint"=>"number","bigint"=>"number","float"=>"float"];
if (isset($rule[$type])) {
return "|".$rule[$type];
}else{
return false;
}
}
public static function _update($updata){
if (!is_array($updata)) {
return ['status'=>false,'msg'=>'参数传输错误'];
}
$table_info = self::getTableInfo();
$table_name = self::getTable();//获取当前模型数据表名
$rule = [];
$cache_table_info = self::query("SHOW COLUMNS FROM ".$table_name);
$table_default = [];
foreach ($cache_table_info as $key => $value) {
$table_default[$value['Field']] = $value;
}
foreach ($table_info['fields'] as $key => $value) {
if ($table_default[$value]['Key'] != "PRI") {
$str = $table_info['type'][$value];
preg_match_all("/[^\(]*\(\D*(\d*).*/i",$str,$str_ary);
$type = @explode("(", $str)["0"];
$type_rule = self::_getTypeRule($type);
$maxvalue = @intval($str_ary['1']['0']);
if ($maxvalue == 0) {
//如果是text没有最大限制的话默认限制20000字符
$maxvalue = 20000;
}
//判断该字段是否设置了非空
if ($table_default[$value]['Default'] == "" && $table_default[$value]['Null'] == 'NO') {
$rule[$value] = "require|max:".$maxvalue.$type_rule;
}else{
$rule[$value] = "max:".$maxvalue.$type_rule;
}
}
}
$data = [];
foreach ($table_default as $key => $value) {
if ($value['Key'] != "PRI") {
$data[$value['Field']] = isset($updata[$value['Field']])?$updata[$value['Field']]:"";
}
}
$pk = $table_info['pk'];//当前数据表主键
$validate = new Validate($rule);
if (!$validate->check($data)) {
return ['status'=>false,'msg'=>$validate->getError()];
}else{
$return = false;
if (isset($updata[$pk])?$updata[$pk]:0 > 0) {
$return = self::where($pk,$updata[$pk])->update($data);
}else{
$return = self::insertGetId($data);
}
if ($return) {
return ['status'=>true,'msg'=>'操作成功'];
}else{
return ['status'=>false,'msg'=>'操作失败'];
}
}
}
}
?>
在其他地方使用方法:// Add
public function add(){
$model = new BodiesModel();
if ($this->request->isPost()) {
$updata = $this->request->post();
$return = $model->_update($updata);
if ($return['status'] == true) {
return $this->success($return['msg'],url('index'));
}else{
return $this->error($return['msg']);
}
}else{
$this->assign('area',AreaModel::where('area_parent_id',0)->select());
$this->assign('major_list',MajorModel::where('major_status',1)->select());
return $this->fetch('update_');
}
}
// Edit
public function edit($id){
$bodies_id = intval($id)==0?exit($this->error('参数错误')):intval($id);
$model = new BodiesModel();
$data = $model->where('bodies_id',$bodies_id)->find();
if (empty($data)) {
return $this->error('数据不存在');
}
if ($this->request->isPost()) {
$updata = $this->request->post();
$updata['bodies_id'] = $bodies_id;
$return = $model->_update($updata);
if ($return['status'] == true) {
return $this->success($return['msg'],url('index'));
}else{
return $this->error($return['msg']);
}
}else{
$this->assign('area',AreaModel::where('area_parent_id',0)->select());
$this->assign('major_list',MajorModel::where('major_status',1)->select());
$this->assign('data',$data);
return $this->fetch('update_');
}
}
有问题欢迎在下方评论区留言 也可以加QQ交流:644332569,本人博客:新疆网站建设