tp2.2支持子查询

jerry 2015年11月20日 收藏
使tp2.2支持子查询,比较上线的项目 没办法总升级框架....tp3.0以下不能用子查询 比较无语了  更改后使用方法和tp3.0一样
更改/ThinkPHP/Lib/Think/Db/Db.class.php和/ThinkPHP/Lib/Think/Core/Model.class.php

懒人给了压缩包 tp2.1和2.2通用
直接解压到/ThinkPHP/Lib/Think/Core 目录下


Model.class.php select函数改为
  1. public function select($options=array()) {
            if(is_string($options) || is_numeric($options)) {
                // 根据主键查询
                $pk   =  $this->getPk();
                if(strpos($options,',')) {
                    $where[$pk] =  array('IN',$options);
                }else{
                    $where[$pk]   =  $options;
                }
                $options =  array();
                $options['where'] =  $where;
            }elseif(false === $options){
                $options =  array();
                // 分析表达式
                $options =  $this->_parseOptions($options);
                return  '( '.$this->db->buildSelectSql($options).' )';
            }
            // 分析表达式
            $options =  $this->_parseOptions($options);
            $resultSet = $this->db->select($options);
            if(false === $resultSet) {
                return false;
            }
            if(empty($resultSet)) { // 查询结果为空
                return null;
            }
            $this->_after_select($resultSet,$options);
            return $resultSet;
        }
Db.class.php  parseTable函数改为
  1. protected function parseTable($tables) {
            if(is_array($tables)) {// 支持别名定义
                $array   =  array();
                foreach ($tables as $table=>$alias){
                    if(!is_numeric($table))
                        $array[] =  $this->parseKey($table).' '.$this->parseKey($alias);
                    else
                        $array[] =  $this->parseKey($table);
                }
                $tables  =  $array;
            }elseif(is_string($tables)){
                $tables  =  explode(',',$tables);
                //array_walk($tables, array(&$this, 'parseKey'));//支持子查询 取消过滤
            }
            return implode(',',$tables);
        }