背景:通过判断不同的请求参数,返回不同的需要的数据
一、准备工作:
数据库新建一个表
DROP TABLE IF EXISTS `tb_category2`; CREATE TABLE IF NOT EXISTS `tb_category2` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键', `category_id` int(10) UNSIGNED DEFAULT NULL COMMENT '一级品类id', `ename` varchar(64) NOT NULL COMMENT '英文名', `cname` varchar(64) NOT NULL COMMENT '中文名', `seq` int(10) UNSIGNED NOT NULL COMMENT '排序序号', PRIMARY KEY (`id`), UNIQUE key `category_id_index` (`category_id`) ) ENGINE=InnoDB AUTO_INCREMENT = 0 DEFAULT CHARSET=utf8 COMMENT = '二级业务品类'; // 如果主键设置了自增 末尾不必再设置AUTO_INCREMENT 或者 AUTO_INCREMENT = 0
导入数据:
INSERT INTO `tb_category2` (`category_id`, `ename`, `cname`, `seq`) VALUES ( '1', 'MOBA', 'MOBA', '1'), ( '1', '战争策略', '战争策略', '2') // UNIQUE 表示为唯一值,category_id设置唯一后插入相同值会报错!需要删除上面的设置
二、新建调用方法:
1.五类路由:获取/搜索、新增、修改、设置(无新建、有修改)、删除
Route::post('business/getCategory2Info', 'ProjectControllerBusinessManageBusinessCategory2@getCategory2Info'); Route::post('business/addCategory2Info', 'ProjectControllerBusinessManageBusinessCategory2@addCategory2Info'); Route::post('business/updateCategory2Info', 'ProjectControllerBusinessManageBusinessCategory2@updateCategory2Info'); Route::post('business/setCategory2Info', 'ProjectControllerBusinessManageBusinessCategory2@setCategory2Info'); Route::post('business/delCategory2Info', 'ProjectControllerBusinessManageBusinessCategory2@delCategory2Info');
2.直接调用的方法:用于获取请求参数
<?php namespace AppHttpControllersProjectControllerBusinessManage;//命名空间 use AppHttpControllersProjectModelBusinessManageCategory2Info;//间接操作 use AppHttpControllersProjectModelJsonRequest; //格式化数据 // 文件名 = 类名 (否则报错) class BusinessCategory2 { //构建实例 private $json_request; private $categoryInfo; public function __construct() { $this->json_request = resolve(JsonRequest::class); $this->category2Info = new Category2Info();//统一请求,定义在Database外部 } //获取/搜索 public function getCategory2Info() { $category2_id = $this->json_request->get( 'category2_id'); $res = $this->category2Info->getTbCategory2Info($category2_id); return response()->success($res); } //新增:没有对象的概念,建议单独传参数 public function addCategory2Info() { $params = [] $params['category2_id'] = $this->json_request->get( 'category2_id'); $params['ename'] = $this->json_request->get( 'ename'); $params['cname'] = $this->json_request->get( 'cname'); $res = $this->category2Info->addTbCategory2Info($params); return response()->success($res); } //修改:同上 public function updateCategory2Info() { $params = [] $params['id'] = $this->json_request->get( 'id'); $params['category2_id'] = $this->json_request->get( 'category2_id'); $params['ename'] = $this->json_request->get( 'ename'); $params['cname'] = $this->json_request->get( 'cname'); $res = $this->category2Info->updateTbCategory2Info($params); return response()->success($res); } //设置:新建或修改 public function setCategory2Info() { $params_list = $this->json_request->get( 'params_list'); $res = $this->category2Info->setTbCategory2Info( $params_list); return response()->success($res); } //删除 public function delCategory2Info() { $id = $this->json_request->get('id'); $res = $this->category2Info-> delTbCategory2Info($id); return response()->success($res); } }
3.中间件处理:用于处理相关表的逻辑,如不可删除已关联的数据
方式1:路由中拦截(middleware()是全局方法,如权限控制)
Route::post('business/getCategoryInfo', 'ProjectControllerBusinessManageBusinessCategory@getCategoryInfo')->middleware('WriteableMiddleware:related');
方式2:中间件处理
<?php namespace AppHttpControllersProjectModelBusinessManage; use AppHttpControllersProjectModelBusinessManageDatabaseTbCategory2Info; use AppHttpControllersProjectModelActivityManageLogicActivityList; use AppHttpControllersProjectModelCommonProtocolErrorsLogicError; class Category2Info { private $TbCategory2Info; public function __construct() { $this->tbCategory2Info = new TbCategory2Info(); } /** * 获取 / 搜索 * @param null / string * @return array **/ public function getTbCategory2Info($category2_id) { $info = $this->tbCategory2Info->getCategory2Info($category2_id); return $info; } /** * 新增:递增到最后一列 * @param object * @return object **/ public function addTbCategory2Info($params) { $info = $this->tbCategory2Info->addCategory2Info($params); return $info; } /** * 修改:某一列 * @param object * @return object **/ public function updateTbCategory2Info($params) { $info = $this->tbCategory2Info->updateCategory2Info($params); return $info; } /** * 设置:遍历新建或修改 * @param array * @return array **/ public function setTbCategory2Info($params_list) { $info = $this->tbCategory2Info->setCategory2Info($params_list); return $info; } /** * 删除:【添加表的关联限制】 * @param string / number * @return object **/ public function delTbCategory2Info($category2_id) { $activityClass = resolve_class(ActivityList::class); $activityList = $activityClass::search(["activity.category2.id"=>$category2_id]); if ( !$activityList->isEmpty() ) { throw new Error("有活动引用,需要先删除活动"); }else{ return $this->tbCategory2Info->delCategory2Info($category2_id); } } } ?>
3.表的实际操作:
<?php namespace AppHttpControllersProjectModelBusinessManageDatabase; use AppHttpControllersProjectModelCommonDatabaseEloBusinessBaseDatabase; //父类 class TbCategory2Info extends EloBusinessBaseDatabase { //表名 protected $table = "tb_category2"; //主键名 protected $primaryKey = "id"; //主键是自增id public $incrementing = true; //所有字段可以批量修改 protected $guarded = []; /** * 获取 / 搜索 * @param null / string * @return array **/ public function getCategory2Info($id) { $ret = []; if ($id) { //是否为空 $ret = static::where("id", $id)->get(); } else { //获取所有品类id $ret = static::all(); } return $ret;//返回的内容 } /** * 新增:递增到最后一列 * @param object * @return object **/ public function addCategory2Info($params) { if($params['category_id'] && $params['ename'] && $params['cname']){ return static::create( $params); } else { return "请求参数不全"; } } /** * 修改:某一列 * @param object * @return object **/ public function updateCategory2Info($params) { if(!$params['id'] || !$arams['category_id'] || !$params['ename'] || !$params['cname']){ return "请求参数不全"; } else { return static::where( "id", $business_id)->update($info); } } /** * 设置:遍历新建或修改 * @param array * @return array **/ public function setCategory2Info($param) { foreach ($param as $item) { // 方式1: if (isset($item->id)) { //id存在:修改 static::where("id", $item["id"])->update($item); } else { //创建 static::create($item); } // 方式2: // $newItem = static::firstOrNew(["id" => $item->id]);// 返回某一条实例{xxx} // $newItem->category_id = $item->category_id; // $newItem->ename = $item->ename; // $newItem->cname = $item->cname; // $newItem->save(); } return static::all(); //返回所有参数 } /** * 删除: * @param string / number * @return object **/ public function delCategory2Info($id) { if($id){ return static::where( "id", $id)->delete(); } else { return "参数不可为空"; } } } ?>
整合与优化:
<?php namespace AppHttpControllersProjectModelBusinessManageDatabase; use AppHttpControllersProjectModelCommonDatabaseEloBusinessBaseDatabase; //父类 class TbCategory2Info extends EloBusinessBaseDatabase { //表名 protected $table = "tb_category2"; //主键名 protected $primaryKey = "id"; //主键是自增id public $incrementing = true; //所有字段可以批量修改 protected $guarded = []; /** * 获取 / 搜索 * @param null / string * @return array **/ public function getCategory2Info($id) { $ret = []; if ($id) { //是否为空 $ret = static::where("id", $id)->get(); } else { //获取所有品类id $ret = static::all(); } return $ret;//返回的内容:无参数问题 } /** * 新增:递增到最后一列 * @param object * @return object **/ public function addCategory2Info($params) { if($params['category_id'] && $params['ename'] && $params['cname']){ return static::create( $params); } else { return "请求参数不全"; } } /** * 修改:某一列 * @param object * @return object **/ public function updateCategory2Info($params) { if($params['id'] && $params['category_id'] && $params['ename'] && $params['cname']){ return static::create( $params); } else { return "请求参数不全"; } } /** * 设置:遍历新建或修改 * @param array * @return array **/ public function setCategory2Info($arr) { if(count($arr) > 0){ foreach ($arr as $item) { if (isset($item->id)) { //id存在:修改 static::where("id", $item->id)->update($item); } else { //创建 static::create($item); } } return static::all(); //返回所有 } else { return "请求参数array不可为空"; } } /** * 删除: * @param string / number * @return object **/ public function delCategory2Info($id) { if($id){ return static::where( "id", $id)->delete(); } else { return "请求参数id不可为空"; } } } ?>
搜索优化:
/** * 获取 / 搜索 * @param null / string * @return array **/ public function getCategory2Info($keyword) { $allInfo = static::all(); if ($keyword) { //是否为空 // 方式1 $keyWordInfo = []; // foreach ($allInfo as $curInfo){ // if (strpos($curInfo["ename"], $keyWord) !== false || strpos($curInfo["cname"], $keyWord) !== false){ // $keyWordInfo[] = $curInfo; // } else { // return []; // } // } // 方式2 $keyWordInfo = static::where([ //多个条件 ['ename', 'like', "%{$keyword}%"], ['cname', 'like', "%{$keyword}%"], ])->get(); return $keyWordInfo;//返回的内容 }else{ return $allInfo;//返回的内容 } }
返回报错优化:请求参数有误需要中止返回 throw new LogicError("xxx");
<?php namespace AppHttpControllersProjectModelBusinessManageDatabase; use AppHttpControllersProjectModelCommonDatabaseEloBusinessBaseDatabase; //父类 use AppHttpControllersProjectModelCommonProtocolErrorsLogic; class TbCategory2Info extends EloBusinessBaseDatabase { /** * 删除: * @param string / number * @return object **/ public function delCategory2Info($id) { if($id){ return static::where( "id", $id)->delete(); } else { throw new LogicError("请求参数id不可为空"); } } } ?>
输出方法集:https://www.cnblogs.com/wheatCatcher/p/11215832.html
php语法:https://www.php.net/manual/zh/language.types.array.php
-end-