thinkphp _complex 复合查询 where多个子组实现

SELECT * FROM `user` WHERE (  `mobile` = '13824653465' OR `nickname` = 'evan' OR `openid` = '14545-fdes'  ) AND (  `bind_seller` = 'fdaflj24214'  OR `seller_type` = '1'  ) AND ( `reg_time` < 12324568  AND  `login_count` > 10  )

  THINKPHP 数组where实现多个复合查询,例如上面的sql。查询官方手册,感觉无法实现。于是想到修改一下THINKPHP框架的DB类。

thinkphp 3.1 文件所在 \ThinkPHP\Lib\Core\Db.class.php

tthinkphp 3.2  \ThinkPHP\Library\Think\Db\Driver.class.php

修改这个方法

 1     protected function parseThinkWhere($key,$val) {
 2         $whereStr   = '';
 3         switch($key) {
 4             case '_string':
 5                 // 字符串模式查询条件
 6                 $whereStr = $val;
 7                 break;
 8             case '_complex':
 9             case '_complex1': //增加多个case分支,支持多个符合 只查询
10             case '_complex2':
11             case '_complex3':
12             case '_complex4':13                 // 复合查询条件
14                 $whereStr = substr($this->parseWhere($val),6);
15                 break;
16             case '_query':
17                 // 字符串模式查询条件
18                 parse_str($val,$where);
19                 if(isset($where['_logic'])) {
20                     $op   =  ' '.strtoupper($where['_logic']).' ';
21                     unset($where['_logic']);
22                 }else{
23                     $op   =  ' AND ';
24                 }
25                 $array   =  array();
26                 foreach ($where as $field=>$data)
27                     $array[] = $this->parseKey($field).' = '.$this->parseValue($data);
28                 $whereStr   = implode($op,$array);
29                 break;
30         }
31         return $whereStr;
32     }

 

 

 

 $      where['mobile'] = '13824653465';
        $where['_logic']  = 'or';
        $where['nickname'] = 'evan';
        $where['openid']  = '14545-fdes';



        $where2['bind_seller'] = 'fdaflj24214';
        $where2['seller_type'] = '1';
        $where2['_logic']      = 'or';

        $where3['reg_time']    = array('lt',12324568);
        $where3['login_count'] = array('gt',10);

        $map['_complex'] = $where;
        $map['_complex1']= $where2;
        $map['_complex2']= $where3;

        $sql = M('User')->where($map)->select();
        echo M()->getLastSql();

  

posted @ 2017-12-08 16:20  胡国洪  阅读(4461)  评论(0编辑  收藏  举报