使用超轻量级框架HYphp,过程中遇到了关于根据条件排序,以及获取limit的条数的数据问题,Model模型代码不太完善或者自身不是太会用,小灰灰添加了部分代码以处理以上问题。
打开文件:
HY->Model.php
找到代码:public $table;
在下面添加:
public $prefixme;
找到代码:$this->pdo = $pdo_obj[$key] ;
在下面添加:$this->prefixme = $prefix;
目的:添加代码获取数据库的表前缀。。。
找到代码:
public function id(){
return $this->pdo->id();
}
在下面添加代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //添加查询代码支持:排序orderby以及limit public function query_limit($by='desc',$litp=0,$litn=2,$where = null){ $sql ='select * from '.$this->prefixme.$this->table. $where.' order by '.$by.' limit ' .$litp.','.$litn; return $this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); } //添加查询代码支持:分组groupby 排序orderby 以及limit public function query_limitNoRep($by='desc',$litp=0,$litn=2,$where = null,$repkey){ $sql ='select * from '.$this->prefixme.$this->table.$where.' GROUP BY '.$repkey.' order by '.$by.' limit ' .$litp.','.$litn; //echo $sql; //echo 'select * from t_personnelfiles where pf_workingState = 2 GROUP BY pf_fileNumber ORDER BY pf_id LIMIT 0,20'; return $this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); } //添加查询代码支持:根据条件查询 public function query_limit2($where = null){ $sql ='select * from '.$this->prefixme.$this->table.' where '. $where; return $this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); } |
完整代码为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | <?php namespace HY; use PDO; /** * Model.php Model */ // class HY_MODEL{ // static public $pdo =false; // static public $database; // } class Model { public $pdo = false; public $table; public $prefixme; function __construct($name,$more_sql_name = '') { static $pdo_obj =array(); static $prefix; $sql_more_bool = false; if($more_sql_name){ $sql_more = C('SQL_MORE.'.$more_sql_name); if(!empty($sql_more)) $sql_more_bool =true; } $database_type = $sql_more_bool ? $sql_more['SQL_TYPE'] : C("SQL_TYPE"); $database_name = $sql_more_bool ? $sql_more['SQL_NAME'] : C("SQL_NAME"); $server = $sql_more_bool ? $sql_more['SQL_IP'] : C("SQL_IP"); $key = $more_sql_name;//substr(md5($database_type . $database_name . $server),0,6); $this->table = $name; if(!isset($pdo_obj[$key])){ $username = $sql_more_bool ? $sql_more['SQL_USER'] : C("SQL_USER"); $password = $sql_more_bool ? $sql_more['SQL_PASS'] : C("SQL_PASS"); $charset = $sql_more_bool ? $sql_more['SQL_CHARSET'] : C("SQL_CHARSET"); $port = $sql_more_bool ? $sql_more['SQL_PORT'] : C("SQL_PORT"); $prefix = $sql_more_bool ? strtolower($sql_more['SQL_PREFIX']) : strtolower(C("SQL_PREFIX")); $option = $sql_more_bool ? $sql_more['SQL_OPTION'] : C("SQL_OPTION"); $a = microtime(TRUE); $pdo_obj[$key] = new \HY\Lib\Medoo(array( // 必须配置项 'database_type' => $database_type, 'database_name' => $database_name, 'server' => $server, 'username' => $username, 'password' => $password, 'charset' => $charset, // 可选参数 'port' => $port, // 可选,定义表的前缀 'prefix' => $prefix, // 连接参数扩展, 更多参考 http://www.php.net/manual/en/pdo.setattribute.php 'option' => $option, )); $GLOBALS['SQL_LOG'][] = '连接'.$database_type.'::'.$database_name.'数据库 [耗时] ' .round(microtime(TRUE) - $a, 4).'ms'; } $this->pdo = $pdo_obj[$key] ; $this->prefixme = $prefix; } // 插入数据 array('user'=>$user) public function insertAll($columns ,$datas){ return $this->pdo->insertAll($this->table,$columns, $datas); } public function insert($data){ return $this->pdo->insert($this->table, $data); } //查询数据 要查询的字段名. 查询的条件. public function select($join, $columns = null, $where = null){ return $this->pdo->select($this->table,$join,$columns,$where); } public function update($data, $where=null){ return $this->pdo->update($this->table,$data,$where); } public function delete($where){ return $this->pdo->delete($this->table,$where); } public function get($join = null, $column = null, $where = null){ return $this->pdo->get($this->table,$join,$column,$where); } public function find($join = null, $column = null, $where = null){ return $this->pdo->get($this->table,$join,$column,$where); } public function replace($columns, $search = null, $replace = null, $where = null){ return $this->pdo->replace($this->table,$columns, $search, $replace, $where); } public function has($join, $where=null){ return $this->pdo->has($this->table,$join, $where); } public function count($join=null, $column=null, $where=null){ return $this->pdo->count($this->table,$join, $column, $where); } public function max($join, $column = null, $where = null){ return $this->pdo->max($this->table,$join, $column, $where); } public function min($join, $column = null, $where = null){ return $this->pdo->min($this->table, $join, $column, $where); } public function avg($join, $column = null, $where = null){ return $this->pdo->avg($this->table, $join, $column, $where); } public function sum($join, $column = null, $where = null){ return $this->pdo->sum($this->table, $join, $column, $where); } public function action($actions){ return $this->pdo->action($actions); } public function query($query){ return $this->pdo->query($query); } public function quote($string){ return $this->pdo->quote($string); } public function debug(){ $this->pdo->debug(); return $this; } public function id(){ return $this->pdo->id(); } //添加查询代码支持:排序orderby以及limit public function query_limit($by='desc',$litp=0,$litn=2,$where = null){ $sql ='select * from '.$this->prefixme.$this->table. $where.' order by '.$by.' limit ' .$litp.','.$litn; return $this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); } //添加查询代码支持:分组groupby 排序orderby 以及limit public function query_limitNoRep($by='desc',$litp=0,$litn=2,$where = null,$repkey){ $sql ='select * from '.$this->prefixme.$this->table.$where.' GROUP BY '.$repkey.' order by '.$by.' limit ' .$litp.','.$litn; //echo $sql; //echo 'select * from t_personnelfiles where pf_workingState = 2 GROUP BY pf_fileNumber ORDER BY pf_id LIMIT 0,20'; return $this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); } //添加查询代码支持:根据条件查询 public function query_limit2($where = null){ $sql ='select * from '.$this->prefixme.$this->table.' where '. $where; return $this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); } } |