/#/lib/class.model.php

https://bitbucket.org/nemo_xiaolan/muyou · PHP · 279 lines · 187 code · 44 blank · 48 comment · 22 complexity · 7030c46904405308ce841718fde89748 MD5 · raw file

  1. <?php
  2. class BaseModel{
  3. protected $table;
  4. protected $db = null;
  5. /**
  6. * 实例化数据库连接
  7. * */
  8. public function __construct() {
  9. $this->db = PdoBackend::get_instance();
  10. $this->table = $this->get_table($this->table);
  11. }
  12. /**
  13. * 插入
  14. * @param array $data
  15. * @return int last-insert-id
  16. * */
  17. public function insert($data) {
  18. $sql = "INSERT INTO {$this->table}(%s)VALUES(%s)";
  19. $keys = array_keys($data);
  20. $values_bind = array();
  21. foreach($keys as $v) {
  22. $values_bind[$v] = ":".$v;
  23. }
  24. $sql = sprintf($sql, implode(',', $keys), implode(',', $values_bind));
  25. $binds = array();
  26. foreach($values_bind as $k=>$v) {
  27. $binds[$v] = $data[$k];
  28. }
  29. $stmt = $this->db->prepare($sql);
  30. $rs = $stmt->execute($binds);
  31. return $this->db->lastInsertId();
  32. }
  33. /**
  34. * 编辑
  35. * @param int $id
  36. * @param array $data
  37. * @return boolean
  38. * */
  39. public function edit($id, $data) {
  40. $sql = "UPDATE {$this->table} SET %s WHERE id=%d";
  41. foreach($data as $k=>$v) {
  42. $sql_placeholder[] = sprintf("%s=:%s", $k, $k);
  43. $values_bind[":".$k] = $v;
  44. }
  45. $sql = sprintf($sql, implode(",", $sql_placeholder), $id);
  46. $stmt = $this->db->prepare($sql);
  47. $rs = $stmt->execute($values_bind);
  48. return $this->db->lastInsertId();
  49. }
  50. /**
  51. * 多条件更新
  52. * */
  53. public function update($condition, $data) {
  54. $condition = $this->get_condition($condition);
  55. foreach($data as $k=>$v) {
  56. if(!$k) {
  57. continue;
  58. }
  59. $update[] = sprintf('%s="%s"', $k, $v);
  60. }
  61. if($update) {
  62. $sql = sprintf("UPDATE {$this->table} SET %s {$condition}", implode(",", $update));
  63. $this->db->query($sql);
  64. return $this->db->lastInsertId();
  65. }
  66. return true;
  67. }
  68. /**
  69. * 根据单一条件删除
  70. * */
  71. public function delete($value, $field="id") {
  72. $sql = "DELETE FROM {$this->table} WHERE {$field}=:value";
  73. $stmt = $this->db->prepare($sql);
  74. return $stmt->execute(array(":value"=> $value));
  75. }
  76. /**
  77. * 执行插入/编辑
  78. * */
  79. public function replace($data, $id = 'null', $primary_key = "id") {
  80. if(!$data[$primary_key] and false !== $id) {
  81. $data[$primary_key] = $id;
  82. }
  83. $sql = "REPLACE INTO {$this->table}(%s)VALUES(%s)";
  84. $keys = array_keys($data);
  85. $values_bind = array();
  86. foreach($keys as $v) {
  87. $values_bind[$v] = ":".$v;
  88. }
  89. $sql = sprintf($sql, implode(',', $keys), implode(',', $values_bind));
  90. $binds = array();
  91. foreach($values_bind as $k=>$v) {
  92. $binds[$v] = $data[$k];
  93. }
  94. $stmt = $this->db->prepare($sql);
  95. $rs = $stmt->execute($binds);
  96. return $this->db->lastInsertId();
  97. }
  98. /**
  99. * 根据某个单一字段条件获取一条或者多条数据
  100. * @param string $field
  101. * @param mixed $value
  102. * @param boolean $getone
  103. * @param boolean | integer $cache 是否缓存/缓存存活时间
  104. * @return array
  105. * */
  106. public function get_by($value, $field="id", $getone = true) {
  107. $sql = "SELECT * FROM `{$this->table}` WHERE %s = '%s'";
  108. $sql = sprintf($sql, $field, $value);
  109. $method = $getone ? 'fetch' : 'fetchAll';
  110. $result = $this->db->query($sql);
  111. if($result) {
  112. $result = $result->$method(PDO::FETCH_ASSOC);
  113. }
  114. return $result;
  115. }
  116. /**
  117. * 获取列表
  118. * @param array $condition
  119. * @param string $order
  120. * */
  121. public function get_list($condition=array(), $order="id DESC", $table = '') {
  122. $table = $table ? $this->get_table($table) : $this->table;
  123. $condition_str = $this->get_condition($condition);
  124. $this->sql = sprintf("SELECT * FROM {$table} %s", $condition_str);
  125. if($order) {
  126. $sql.= " ORDER BY ".$order;
  127. }
  128. if($rs = $this->db->query($this->sql)) {
  129. return $rs->fetchAll(PDO::FETCH_ASSOC);
  130. }
  131. return array();
  132. }
  133. /**
  134. * 获取分页
  135. * @param array $condition
  136. * @param string $order
  137. * @param integer $size 每页显示的数量
  138. */
  139. public function get_page_list($condition=array(), $oder="id DESC", $size=20) {
  140. $condition = $this->get_condition($condition);
  141. $sql = sprintf("SELECT * FROM {$this->table} %s", $condition);
  142. if($order) {
  143. $sql.= " ORDER BY ".$order;
  144. }
  145. $total = $this->count($condition);
  146. import("lib/class.paginator");
  147. $p = new Paginator($this->db, $_GET["page"], $size);
  148. return $p->work($sql, $total);
  149. }
  150. /**
  151. * 返回表名
  152. * */
  153. public function get_table($table = null) {
  154. if(!$table) {
  155. return $this->table;
  156. }
  157. global $C;
  158. return $C["database"]["pre"].$table;
  159. }
  160. /**
  161. * 根据数组获取SQL WHERE
  162. * */
  163. public function get_condition($condition = array()) {
  164. $operation = array(
  165. "!" => "!=",
  166. ">" => ">",
  167. "<" => "<",
  168. "#" => ">=",
  169. "$" => "<=",
  170. "^" => " IN "
  171. );
  172. $condition_str = " WHERE TRUE";
  173. if($condition) {
  174. foreach($condition as $k=>$v) {
  175. if(!$k) {
  176. continue;
  177. }
  178. $split = "=";
  179. if(is_array($v)) {
  180. foreach($v as $_k=>$_v) {
  181. if(!$_k) {
  182. continue;
  183. }
  184. $_split = "=";
  185. if(key_exists($_v{0}, $operation)) {
  186. $_split = $operation[$_v{0}];
  187. $_v = substr($_v, 1, strlen($_v));
  188. }
  189. $sub_conditions[] = sprintf("%s%s'%s'", $k, $_split, $_v);
  190. }
  191. } else {
  192. if(key_exists($v{0}, $operation)) {
  193. $split = $operation[$v{0}];
  194. $v = substr($v, 1, strlen($v));
  195. }
  196. $conditions[] = sprintf("%s%s'%s'", $k, $split, $v);
  197. }
  198. }
  199. $condition_str = '';
  200. if($sub_conditions) {
  201. $conditions[] = '('.implode(' OR ', $sub_conditions).')';
  202. }
  203. if($conditions) {
  204. $condition_str = ' WHERE '.implode(" AND ", $conditions);
  205. }
  206. }
  207. $condition_str = str_replace(
  208. array("'(", ")'"),
  209. array("(", ")"),
  210. $condition_str
  211. );
  212. return $condition_str;
  213. }
  214. public function count($condition = "", $field = "id", $table = "") {
  215. $condition = is_array($condition) ? $this->get_condition($condition) : $condition;
  216. if($table) {
  217. global $C;
  218. if(false === strpos($table, $C["database"]["pre"])) {
  219. $table = $this->get_table($table);
  220. } else {
  221. $table = $table;
  222. }
  223. } else {
  224. $table = $this->table;
  225. }
  226. $sql = sprintf("SELECT COUNT(%s) AS count FROM {$table} %s", $field, $condition);
  227. $this->sql = $sql;
  228. $rs = $this->db->query($sql);
  229. if(!$rs) {
  230. return 0;
  231. }
  232. $count = $rs->fetch(PDO::FETCH_ASSOC);
  233. return $count['count'];
  234. }
  235. }