thinkphp中的select方法与find方法区别

  1. find方法源码,发现find方法只会返回一条记录,有Limit 限制,并且是一维数组,php

  2. /**
         * 查询数据
         * @access public
         * @param mixed $options 表达式参数
         * @return mixed
         */
        public function find($options=array()) {
            if(is_numeric($options) || is_string($options)) {
                $where[$this->getPk()]  =   $options;
                $options                =   array();
                $options['where']       =   $where;
            }
            // 根据复合主键查找记录
            $pk  =  $this->getPk();
            if (is_array($options) && (count($options) > 0) && is_array($pk)) {
                // 根据复合主键查询
                $count = 0;
                foreach (array_keys($options) as $key) {
                    if (is_int($key)) $count++; 
                } 
                if ($count == count($pk)) {
                    $i = 0;
                    foreach ($pk as $field) {
                        $where[$field] = $options[$i];
                        unset($options[$i++]);
                    }
                    $options['where']  =  $where;
                } else {
                    return false;
                }
            }
            // 老是查找一条记录
            $options['limit']   =   1;
            // 分析表达式
            $options            =   $this->_parseOptions($options);
            // 判断查询缓存
            if(isset($options['cache'])){
                $cache  =   $options['cache'];
                $key    =   is_string($cache['key'])?$cache['key']:md5(serialize($options));
                $data   =   S($key,'',$cache);
                if(false !== $data){
                    $this->data     =   $data;
                    return $data;
                }
            }
            $resultSet          =   $this->db->select($options);
            if(false === $resultSet) {
                return false;
            }
            if(empty($resultSet)) {// 查询结果为空
                return null;
            }
            if(is_string($resultSet)){
                return $resultSet;
            }
    
            // 读取数据后的处理
            $data   =   $this->_read_data($resultSet[0]);
            $this->_after_find($data,$options);
            if(!empty($this->options['result'])) {
                return $this->returnResult($data,$this->options['result']);
            }
            $this->data     =   $data;
            if(isset($cache)){
                S($key,$data,$cache);
            }
            return $this->data;
        }
  3. select方法源码,查询数据集,返回二维数组,配合volist标签使用!sql

  4. /**
         * 查询数据集
         * @access public
         * @param array $options 表达式参数
         * @return mixed
         */
        public function select($options=array()) {
            $pk   =  $this->getPk();
            if(is_string($options) || is_numeric($options)) {
                // 根据主键查询
                if(strpos($options,',')) {
                    $where[$pk]     =  array('IN',$options);
                }else{
                    $where[$pk]     =  $options;
                }
                $options            =  array();
                $options['where']   =  $where;
            }elseif (is_array($options) && (count($options) > 0) && is_array($pk)) {
                // 根据复合主键查询
                $count = 0;
                foreach (array_keys($options) as $key) {
                    if (is_int($key)) $count++; 
                } 
                if ($count == count($pk)) {
                    $i = 0;
                    foreach ($pk as $field) {
                        $where[$field] = $options[$i];
                        unset($options[$i++]);
                    }
                    $options['where']  =  $where;
                } else {
                    return false;
                }
            } elseif(false === $options){ // 用于子查询 不查询只返回SQL
            	$options['fetch_sql'] = true;
            }
            // 分析表达式
            $options    =  $this->_parseOptions($options);
            // 判断查询缓存
            if(isset($options['cache'])){
                $cache  =   $options['cache'];
                $key    =   is_string($cache['key'])?$cache['key']:md5(serialize($options));
                $data   =   S($key,'',$cache);
                if(false !== $data){
                    return $data;
                }
            }        
            $resultSet  = $this->db->select($options);
            if(false === $resultSet) {
                return false;
            }
            if(!empty($resultSet)) { // 有查询结果
                if(is_string($resultSet)){
                    return $resultSet;
                }
    
                $resultSet  =   array_map(array($this,'_read_data'),$resultSet);
                $this->_after_select($resultSet,$options);
                if(isset($options['index'])){ // 对数据集进行索引
                    $index  =   explode(',',$options['index']);
                    foreach ($resultSet as $result){
                        $_key   =  $result[$index[0]];
                        if(isset($index[1]) && isset($result[$index[1]])){
                            $cols[$_key] =  $result[$index[1]];
                        }else{
                            $cols[$_key] =  $result;
                        }
                    }
                    $resultSet  =   $cols;
                }
            }
    
            if(isset($cache)){
                S($key,$resultSet,$cache);
            }
            return $resultSet;
        }

  5. 参考资料数组

  6. http://blog.csdn.net/baronyang/article/details/8674673
    缓存

相关文章
相关标签/搜索