thinkphp5 查询接口的懒人写法

	 	    public function _initialize()
    { 
	 		  parent::_initialize();
        $this->model = new \app\common\model\user\Directory; 
        /* * EQ、= 等于(=)NEQ、<> 不等于(<>)GT、> 大于(>)EGT、>= 大于等于(>=)LT、< 小于(<)ELT、<= 小于等于(<=)LIKE 模糊查询[NOT] BETWEEN (不在)区间查询[NOT] IN (不在)IN 查询[NOT] NULL 查询字段是否(不)是NULL[NOT] EXISTS EXISTS查询EXP 表达式查询,支持SQL语法> time 时间比较< time 时间比较between time 时间比较、notbetween time 时间比较 * */
		 $this->select_datt = [
		 	'class_id'=>"LIKE",
		 	'directory_id'=>"=" 
		 ]; 
    	 
    }
    
    /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */
    
   /** * 查看 */
    public function index()
    {
        //当前是否为关联查询
 	  $where=[];
      $sort=[];
      $order=[]; 
		foreach ($this->select_datt  as $key => $value) {
			
		 
			if(isset($_POST[$key])&&!empty($_POST[$key])){
				if(trim(strtoupper($value)," ")=="LIKE"){
					$where[$key]=[$value,"%".$_POST[$key]."%"];
				}else if(trim(strtoupper($value)," ")=="BETWEEN"||trim(strtoupper($value)," ")=="NOTBETWEEN"){
					$where[$key]=[$value,$_POST[$key]];
				}else if(trim(strtoupper($value)," ")=="IN"||trim(strtoupper($value)," ")=="NOTIN"){
					$where[$key]=[$value,$_POST[$key]];
				}else{
					$where[$key]=$_POST[$key];
				}
				 
			 
				
			}
		}
		$user = $this->auth->getUser(); 
		
		$where["user_id"]=$user->id;
      $page=(isset($_POST["page"])&&!empty($_POST["page"]))?$_POST["page"]:1;
      $limit=isset($_POST["limit"])&&!empty($_POST["limit"])?$_POST["limit"]:10;
            
            $total = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->page($page, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','name']);
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

 
      		 $this->success('请求成功',$result);
       
    }
没有账号? 忘记密码?

社交账号快速登录