/Library/Frameworks/Model.php

https://github.com/GeekZooTeam/Geek-Zoo-PHP-Library · PHP · 402 lines · 292 code · 89 blank · 21 comment · 54 complexity · c065f7760036320ca91abc785f155469 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Geek-Zoo Projects.
  4. *
  5. * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
  6. * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
  7. * @author xuanyan <xuanyan@geek-zoo.com>
  8. *
  9. */
  10. class Model
  11. {
  12. protected $db = null;
  13. public $table = '';
  14. protected $field = array();
  15. protected $pri = '';
  16. public function __construct($table = '')
  17. {
  18. $dbConfig = Config::get('database');
  19. $this->db = Database::connect($dbConfig['connection']);
  20. $this->db->setConfig('initialization', $dbConfig['initialization']);
  21. $this->db->setConfig('tablePreFix', $dbConfig['tablePreFix']);
  22. $this->table = $table;
  23. if (empty($this->table)) {
  24. $thisClass = get_class($this);
  25. $this->table = substr($thisClass, 0, -5);
  26. }
  27. $this->table = '{{'.$this->table.'}}';
  28. $keys = $this->db->getAll("DESCRIBE `$this->table`");
  29. foreach ($keys as $val) {
  30. $this->field[$val['Field']] = $val['Default'];
  31. if ($val['Key'] == 'PRI') {
  32. $this->pri = $val['Field'];
  33. }
  34. }
  35. }
  36. protected function getSql($option)
  37. {
  38. $where = array();
  39. foreach ($option as $key => $val) {
  40. $where[] = "`$key` = ?";
  41. }
  42. return 'WHERE '.implode(' AND ', $where);
  43. }
  44. public function read()
  45. {
  46. $table = $this->table;
  47. $params = func_get_args();
  48. $sql = array_shift($params);
  49. // 主键 id 特殊处理
  50. if (is_numeric($sql)) {
  51. $sql = array($this->pri => $sql);
  52. }
  53. if (is_string($sql)) {
  54. $sql = "SELECT * FROM `$table` $sql";
  55. if ($params) {
  56. if (is_array($params[0])) {
  57. $params = $params[0];
  58. }
  59. return $this->db->getRow($sql, $params);
  60. }
  61. return $this->db->getRow($sql);
  62. }
  63. $params = array_values($sql);
  64. $sql = "SELECT * FROM `$table` ".$this->getSql($sql);
  65. return $this->db->getRow($sql, $params);
  66. }
  67. public function create()
  68. {
  69. $table = $this->table;
  70. $params = func_get_args();
  71. $sql = array_shift($params);
  72. $sql = $this->beforeCreate($sql);
  73. // beforeCreate 终止插入进行
  74. if (!$sql) {
  75. return false;
  76. }
  77. $params = array_values($sql);
  78. $sql = "INSERT INTO `$table` (".implode(', ', array_keys($sql)).') VALUES ('.implode(', ', array_fill(0, count($sql), '?')) . ')';
  79. $result = $this->db->exec($sql, $params);
  80. $id = $this->db->lastInsertId();
  81. // 没有自增id
  82. if (!$id) {
  83. return $result;
  84. }
  85. $new = $this->read($id);
  86. $this->afterCreate($new);
  87. return $id;
  88. }
  89. public function update()
  90. {
  91. $table = $this->table;
  92. $params = func_get_args();
  93. $sql = array_shift($params);
  94. // 主键 id 特殊处理
  95. if (is_numeric($sql)) {
  96. $sql = array($this->pri => $sql);
  97. }
  98. if (is_string($sql)) {
  99. if ($params) {
  100. if (is_array($params[0])) {
  101. $params = $params[0];
  102. }
  103. }
  104. if (strpos($sql, 'WHERE') === false) {
  105. $select = "SELECT * FROM `$table`";
  106. $result = $this->db->getAll($select);
  107. $set = $sql;
  108. } else {
  109. list($set, $where) = explode('WHERE', $sql);
  110. $select = "SELECT * FROM `$table` WHERE $where";
  111. $c = substr_count($select, '?');
  112. $select_param = array();
  113. while ($c) {
  114. $select_param[] = array_pop($params);
  115. $c--;
  116. }
  117. $select_param = array_reverse($select_param);
  118. if ($select_param) {
  119. $result = $this->db->getAll($select, $select_param);
  120. } else {
  121. $result = $this->db->getAll($select);
  122. }
  123. $array = array_merge(array($set), $params);
  124. }
  125. } else {
  126. $select = "SELECT * FROM `$table` ".$this->getSql($sql);
  127. $result = $this->db->getAll($select, array_values($sql));
  128. $array = array_shift($params);
  129. }
  130. foreach ($result as $val) {
  131. $array = $this->beforeUpdate($array, $val);
  132. // beforeUpdate 终止更新
  133. if (!$sql) {
  134. continue;
  135. }
  136. if (isset($array[0])) {
  137. $params = $array;
  138. $set = array_shift($params);
  139. } else {
  140. $params = array_values($array);
  141. $set = array();
  142. foreach ($array as $kk => $vv) {
  143. $set[] = "$kk = ?";
  144. }
  145. $set = 'SET ' . implode(',', $set);
  146. }
  147. $sql = "UPDATE `$table` $set WHERE {$this->pri} = {$val[$this->pri]}";
  148. $this->db->exec($sql, $params);
  149. // 获取最新的记录
  150. $new = $this->read($val[$this->pri]);
  151. if (empty($new)) {
  152. throw new Exception("cant load data: id: {$val[$this->pri]}, table : {$table}");
  153. }
  154. $this->afterUpdate($new, $val);
  155. }
  156. return count($result);
  157. }
  158. public function delete()
  159. {
  160. $table = $this->table;
  161. $params = func_get_args();
  162. $sql = array_shift($params);
  163. // 主键 id 特殊处理
  164. if (is_numeric($sql)) {
  165. $sql = array($this->pri => $sql);
  166. }
  167. // if (!is_array($sql)) {
  168. // if (strlen(intval($sql)) == strlen($sql)) {
  169. // $sql = array($this->pri => $sql);
  170. // }
  171. // }
  172. if (is_string($sql)) {
  173. if ($params) {
  174. if (is_array($params[0])) {
  175. $params = $params[0];
  176. }
  177. }
  178. $sql = "SELECT * FROM `$table` $sql";
  179. if (strpos($sql, '?') !== false) {
  180. $result = $this->db->getAll($sql, $params);
  181. } else {
  182. $result = $this->db->getAll($sql);
  183. }
  184. } else {
  185. $params = $sql;
  186. $sql = "SELECT * FROM `$table` ".$this->getSql($params);
  187. $result = $this->db->getAll($sql, array_values($params));
  188. }
  189. foreach ($result as $val) {
  190. // beforeDelete 终止删除
  191. if ($this->beforeDelete($val) === false) {
  192. continue;
  193. }
  194. $sql = "DELETE FROM `$table` WHERE {$this->pri} = {$val[$this->pri]}";
  195. $this->db->exec($sql);
  196. $this->afterDelete($val);
  197. }
  198. return count($result);
  199. }
  200. public function getList()
  201. {
  202. $table = $this->table;
  203. $pager = null;
  204. $params = func_get_args();
  205. $sql = array_shift($params);
  206. if (!$sql) {
  207. $sql = '';
  208. } elseif (is_object($sql)) {
  209. $pager = $sql;
  210. $sql = '';
  211. } elseif (isset($params[0]) && is_object($params[0])) {
  212. $pager = array_shift($params);
  213. }
  214. if (is_string($sql)) {
  215. if ($params) {
  216. if (is_array($params[0])) {
  217. $params = $params[0];
  218. }
  219. }
  220. } else {
  221. $params = $sql;
  222. $sql = $this->getSql($sql);
  223. }
  224. if ($pager) {
  225. $limit = $pager->setPage()->getLimit();
  226. $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `$table` $sql $limit";
  227. } else {
  228. $sql = "SELECT * FROM `$table` $sql";
  229. }
  230. if ($params) {
  231. $result = $this->db->getAll($sql, $params);
  232. } else {
  233. $result = $this->db->getAll($sql);
  234. }
  235. if (!$pager) {
  236. return $result;
  237. }
  238. $count = $this->db->getOne("SELECT FOUND_ROWS()");
  239. $pager->generate($count);
  240. return array(
  241. 'data' => $result,
  242. 'pager' => $pager
  243. );
  244. }
  245. public function getSum()
  246. {
  247. $table = $this->table;
  248. $params = func_get_args();
  249. $column = array_shift($params);
  250. $sql = array_shift($params);
  251. $sql || $sql = '';
  252. if (is_string($sql)) {
  253. if ($params) {
  254. if (is_array($params[0])) {
  255. $params = $params[0];
  256. }
  257. }
  258. $sql = "SELECT SUM($column) FROM `$table` $sql";
  259. if (strpos($sql, '?') !== false) {
  260. $result = $this->db->getOne($sql, $params);
  261. } else {
  262. $result = $this->db->getOne($sql);
  263. }
  264. } else {
  265. $params = $sql;
  266. $sql = "SELECT SUM($column) FROM `$table` ".$this->getSql($params);
  267. $result = $this->db->getOne($sql, array_values($params));
  268. }
  269. return intval($result);
  270. }
  271. public function getCount()
  272. {
  273. $table = $this->table;
  274. $params = func_get_args();
  275. $sql = array_shift($params);
  276. $sql || $sql = '';
  277. if (is_string($sql)) {
  278. if ($params) {
  279. if (is_array($params[0])) {
  280. $params = $params[0];
  281. }
  282. }
  283. $sql = "SELECT COUNT(*) FROM `$table` $sql";
  284. if (strpos($sql, '?') !== false) {
  285. return $this->db->getOne($sql, $params);
  286. }
  287. return $this->db->getOne($sql);
  288. }
  289. $params = $sql;
  290. $sql = "SELECT COUNT(*) FROM `$table` ".$this->getSql($params);
  291. return $this->db->getOne($sql, array_values($params));
  292. }
  293. protected function beforeCreate($new)
  294. {
  295. return $new;
  296. }
  297. protected function afterCreate($new)
  298. {
  299. }
  300. protected function beforeUpdate($new, $old)
  301. {
  302. return $new;
  303. }
  304. protected function afterUpdate($new, $old)
  305. {
  306. }
  307. protected function beforeDelete($old)
  308. {
  309. }
  310. protected function afterDelete($old)
  311. {
  312. }
  313. }