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
    緩存

相關文章
相關標籤/搜索