Model.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace WY\app\libs;
  3. use WY\app\Config;
  4. use WY\app\libs\Db;
  5. use WY\app\libs\Res;
  6. use WY\app\libs\Req;
  7. use WY\app\libs\Log;
  8. if (!defined('WY_ROOT')) {
  9. exit;
  10. }
  11. class Model
  12. {
  13. public $limits = '';
  14. public $limit = false;
  15. public $offset = false;
  16. public $orderby = '';
  17. public $where = [];
  18. public $fields = '';
  19. public $insertData = [];
  20. public $updateSet = [];
  21. private $db;
  22. public $in = false;
  23. public $left = '';
  24. public $right = '';
  25. public $inner = '';
  26. public $join = '';
  27. public $on = '';
  28. public $groupby = '';
  29. function __construct()
  30. {
  31. $this->prefix = Config::db()['prefix'];
  32. $this->db = Db::getInstance();
  33. $this->db->connect();
  34. $this->res = new Res();
  35. $this->req = new Req();
  36. }
  37. function select($fields = '*')
  38. {
  39. $this->fields = $fields;
  40. return $this;
  41. }
  42. function from($table)
  43. {
  44. $this->table = $this->prefix . $table;
  45. return $this;
  46. }
  47. function limit($limit)
  48. {
  49. $this->limit = $limit;
  50. return $this;
  51. }
  52. function offset($offset)
  53. {
  54. $this->offset = $offset;
  55. return $this;
  56. }
  57. function orderby($orderby)
  58. {
  59. if ($orderby) {
  60. $this->orderby = 'ORDER BY ' . $orderby;
  61. }
  62. return $this;
  63. }
  64. function groupby($groupby)
  65. {
  66. $this->groupby = 'GROUP BY ' . $groupby;
  67. return $this;
  68. }
  69. function where($where)
  70. {
  71. $this->where = $where;
  72. return $this;
  73. }
  74. function sums($fields)
  75. {
  76. $this->sums = $fields;
  77. return $this;
  78. }
  79. function in()
  80. {
  81. if ($this->where) {
  82. $this->in = true;
  83. }
  84. return $this;
  85. }
  86. function on($on)
  87. {
  88. $this->on = $on;
  89. return $this;
  90. }
  91. function join()
  92. {
  93. if ($this->left) {
  94. $this->join = 'LEFT JOIN ' . $this->left . ' ON ' . $this->on;
  95. }
  96. if ($this->right) {
  97. $this->join = 'RIGHT JOIN ' . $this->right . ' ON ' . $this->on;
  98. }
  99. if ($this->inner) {
  100. $this->join = 'INNER JOIN ' . $this->inner . ' ON ' . $this->on;
  101. }
  102. return $this;
  103. }
  104. function left($table)
  105. {
  106. $this->left = $this->prefix . $table;
  107. return $this;
  108. }
  109. function inner($table)
  110. {
  111. $this->inner = $this->prefix . $table;
  112. return $this;
  113. }
  114. function right($table)
  115. {
  116. $this->right = $this->prefix . $table;
  117. return $this;
  118. }
  119. function toSql()
  120. {
  121. if (false !== $this->limit) {
  122. if (false !== $this->offset) {
  123. $this->limits = 'LIMIT ' . $this->offset . ',' . $this->limit;
  124. } else {
  125. $this->limits = 'LIMIT ' . $this->limit;
  126. }
  127. }
  128. $sql = "select " . $this->fields . " from " . $this->table . " " . $this->join . " {where} " . $this->groupby . " " . $this->orderby . " " . $this->limits;
  129. return $sql;
  130. }
  131. function fetchAll()
  132. {
  133. $sql = $this->toSql();
  134. $data = array();
  135. if ($this->where) {
  136. $data = $this->db->parseWhere($this->where);
  137. $sql = str_replace('{where}', $data['where'], $sql);
  138. } else {
  139. $sql = str_replace('{where}', '', $sql);
  140. }
  141. $stm = $this->db->prepare($sql);
  142. if ($data) {
  143. $this->db->bindValue($stm, $data['values']);
  144. }
  145. $this->db->execute($stm);
  146. if ($stm->rowCount()) {
  147. return $this->db->fetchAll($stm);
  148. }
  149. return false;
  150. }
  151. function fetchRow()
  152. {
  153. $sql = $this->toSql();
  154. $data = array();
  155. if ($this->where) {
  156. $data = $this->db->parseWhere($this->where);
  157. $sql = str_replace('{where}', $data['where'], $sql);
  158. } else {
  159. $sql = str_replace('{where}', '', $sql);
  160. }
  161. $stm = $this->db->prepare($sql);
  162. if ($data) {
  163. $this->db->bindValue($stm, $data['values']);
  164. }
  165. $this->db->execute($stm);
  166. if ($stm->rowCount()) {
  167. return $this->db->fetchRow($stm);
  168. }
  169. return false;
  170. }
  171. function insertData(array $insertData)
  172. {
  173. $this->insertData = $insertData;
  174. return $this;
  175. }
  176. function updateSet(array $updateSet)
  177. {
  178. $this->updateSet = $updateSet;
  179. return $this;
  180. }
  181. function insert()
  182. {
  183. if (!$this->insertData) {
  184. return false;
  185. }
  186. $result = $this->db->insert($this->table, $this->insertData);
  187. return $result;
  188. }
  189. function count()
  190. {
  191. $result = $this->db->count($this->table, $this->where);
  192. return $result;
  193. }
  194. function sum()
  195. {
  196. $result = $this->db->sum($this->table, $this->fields, $this->where);
  197. return $result;
  198. }
  199. function delete()
  200. {
  201. if ($this->in) {
  202. $result = $this->db->deleteIn($this->table, $this->where);
  203. } else {
  204. $result = $this->db->delete($this->table, $this->where);
  205. }
  206. return $result;
  207. }
  208. function update()
  209. {
  210. if (!$this->updateSet) {
  211. return false;
  212. }
  213. $result = $this->db->update($this->table, $this->updateSet, $this->where);
  214. return $result;
  215. }
  216. protected function model()
  217. {
  218. return new Model();
  219. }
  220. }
  221. ?>