Skip to content

Instantly share code, notes, and snippets.

@wsk3201
Created July 6, 2015 06:04
Show Gist options
  • Select an option

  • Save wsk3201/b02e4d9df12950727b9b to your computer and use it in GitHub Desktop.

Select an option

Save wsk3201/b02e4d9df12950727b9b to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment