model.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * model.class.php 数据模型基类
  4. *
  5. * @copyright (C) 2005-2010 PHPCMS
  6. * @license http://www.phpcms.cn/license/
  7. * @lastmodify 2010-6-7
  8. */
  9. defined('IN_PHPCMS') or exit('Access Denied');
  10. pc_base::load_sys_class('db_factory', '', 0);
  11. class model {
  12. //数据库配置
  13. protected $db_config = '';
  14. //数据库连接
  15. protected $db = '';
  16. //调用数据库的配置项
  17. protected $db_setting = 'default';
  18. //数据表名
  19. protected $table_name = '';
  20. //表前缀
  21. public $db_tablepre = '';
  22. public function __construct() {
  23. if (!isset($this->db_config[$this->db_setting])) {
  24. $this->db_setting = 'default';
  25. }
  26. $this->table_name = $this->db_config[$this->db_setting]['tablepre'].$this->table_name;
  27. $this->db_tablepre = $this->db_config[$this->db_setting]['tablepre'];
  28. $this->db = db_factory::get_instance($this->db_config)->get_database($this->db_setting);
  29. }
  30. /**
  31. * 执行sql查询
  32. * @param $where 查询条件[例`name`='$name']
  33. * @param $data 需要查询的字段值[例`name`,`gender`,`birthday`]
  34. * @param $limit 返回结果范围[例:10或10,10 默认为空]
  35. * @param $order 排序方式 [默认按数据库默认方式排序]
  36. * @param $group 分组方式 [默认为空]
  37. * @param $key 返回数组按键名排序
  38. * @return array 查询结果集数组
  39. */
  40. final public function select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='') {
  41. if (is_array($where)) $where = $this->sqls($where);
  42. return $this->db->select($data, $this->table_name, $where, $limit, $order, $group, $key);
  43. }
  44. /**
  45. * 查询多条数据并分页
  46. * @param $where
  47. * @param $order
  48. * @param $page
  49. * @param $pagesize
  50. * @return unknown_type
  51. */
  52. final public function listinfo($where = '', $order = '', $page = 1, $pagesize = 20, $key='', $setpages = 10,$urlrule = '',$array = array(), $data = '*') {
  53. $where = to_sqls($where);
  54. $this->number = $this->count($where);
  55. $page = max(intval($page), 1);
  56. $offset = $pagesize*($page-1);
  57. $this->pages = pages($this->number, $page, $pagesize, $urlrule, $array, $setpages);
  58. $array = array();
  59. if ($this->number > 0) {
  60. return $this->select($where, $data, "$offset, $pagesize", $order, '', $key);
  61. } else {
  62. return array();
  63. }
  64. }
  65. /**
  66. * 获取单条记录查询
  67. * @param $where 查询条件
  68. * @param $data 需要查询的字段值[例`name`,`gender`,`birthday`]
  69. * @param $order 排序方式 [默认按数据库默认方式排序]
  70. * @param $group 分组方式 [默认为空]
  71. * @return array/null 数据查询结果集,如果不存在,则返回空
  72. */
  73. final public function get_one($where = '', $data = '*', $order = '', $group = '') {
  74. if (is_array($where)) $where = $this->sqls($where);
  75. return $this->db->get_one($data, $this->table_name, $where, $order, $group);
  76. }
  77. /**
  78. * 直接执行sql查询
  79. * @param $sql 查询sql语句
  80. * @return boolean/query resource 如果为查询语句,返回资源句柄,否则返回true/false
  81. */
  82. final public function query($sql) {
  83. $sql = str_replace('phpcms_', $this->db_tablepre, $sql);
  84. return $this->db->query($sql);
  85. }
  86. /**
  87. * 执行添加记录操作
  88. * @param $data 要增加的数据,参数为数组。数组key为字段值,数组值为数据取值
  89. * @param $return_insert_id 是否返回新建ID号
  90. * @param $replace 是否采用 replace into的方式添加数据
  91. * @return boolean
  92. */
  93. final public function insert($data, $return_insert_id = false, $replace = false) {
  94. return $this->db->insert($data, $this->table_name, $return_insert_id, $replace);
  95. }
  96. /**
  97. * 获取最后一次添加记录的主键号
  98. * @return int
  99. */
  100. final public function insert_id() {
  101. return $this->db->insert_id();
  102. }
  103. /**
  104. * 执行更新记录操作
  105. * @param $data 要更新的数据内容,参数可以为数组也可以为字符串,建议数组。
  106. * 为数组时数组key为字段值,数组值为数据取值
  107. * 为字符串时[例:`name`='phpcms',`hits`=`hits`+1]。
  108. * 为数组时[例: array('name'=>'phpcms','password'=>'123456')]
  109. * 数组的另一种使用array('name'=>'+=1', 'base'=>'-=1');程序会自动解析为`name` = `name` + 1, `base` = `base` - 1
  110. * @param $where 更新数据时的条件,可为数组或字符串
  111. * @return boolean
  112. */
  113. final public function update($data, $where = '') {
  114. if (is_array($where)) $where = $this->sqls($where);
  115. return $this->db->update($data, $this->table_name, $where);
  116. }
  117. /**
  118. * 执行删除记录操作
  119. * @param $where 删除数据条件,不充许为空。
  120. * @return boolean
  121. */
  122. final public function delete($where) {
  123. if (is_array($where)) $where = $this->sqls($where);
  124. return $this->db->delete($this->table_name, $where);
  125. }
  126. /**
  127. * 计算记录数
  128. * @param string/array $where 查询条件
  129. */
  130. final public function count($where = '') {
  131. $r = $this->get_one($where, "COUNT(*) AS num");
  132. return $r['num'];
  133. }
  134. /**
  135. * 将数组转换为SQL语句
  136. * @param array $where 要生成的数组
  137. * @param string $font 连接串。
  138. */
  139. final public function sqls($where, $font = ' AND ') {
  140. if (is_array($where)) {
  141. $sql = '';
  142. foreach ($where as $key=>$val) {
  143. $sql .= $sql ? " $font `$key` = '$val' " : " `$key` = '$val'";
  144. }
  145. return $sql;
  146. } else {
  147. return $where;
  148. }
  149. }
  150. /**
  151. * 获取最后数据库操作影响到的条数
  152. * @return int
  153. */
  154. final public function affected_rows() {
  155. return $this->db->affected_rows();
  156. }
  157. /**
  158. * 获取数据表主键
  159. * @return array
  160. */
  161. final public function get_primary() {
  162. return $this->db->get_primary($this->table_name);
  163. }
  164. /**
  165. * 获取表字段
  166. * @param string $table_name 表名
  167. * @return array
  168. */
  169. final public function get_fields($table_name = '') {
  170. if (empty($table_name)) {
  171. $table_name = $this->table_name;
  172. } else {
  173. $table_name = $this->db_tablepre.$table_name;
  174. }
  175. return $this->db->get_fields($table_name);
  176. }
  177. /**
  178. * 检查表是否存在
  179. * @param $table 表名
  180. * @return boolean
  181. */
  182. final public function table_exists($table){
  183. return $this->db->table_exists($this->db_tablepre.$table);
  184. }
  185. /**
  186. * 检查字段是否存在
  187. * @param $field 字段名
  188. * @return boolean
  189. */
  190. public function field_exists($field) {
  191. $fields = $this->db->get_fields($this->table_name);
  192. return array_key_exists($field, $fields);
  193. }
  194. final public function list_tables() {
  195. return $this->db->list_tables();
  196. }
  197. /**
  198. * 返回数据结果集
  199. * @param $query (mysql_query返回值)
  200. * @return array
  201. */
  202. final public function fetch_array() {
  203. $data = array();
  204. while($r = $this->db->fetch_next()) {
  205. $data[] = $r;
  206. }
  207. return $data;
  208. }
  209. /**
  210. * 返回数据库版本号
  211. */
  212. final public function version() {
  213. return $this->db->version();
  214. }
  215. }