Model.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php namespace Dever;
  2. use Dever;
  3. class Model
  4. {
  5. protected $method = '';
  6. protected $store;
  7. protected $partition = false;
  8. public $config = array();
  9. public function __construct($table, $app, $store, $partition, $path)
  10. {
  11. $project = Project::load($app);
  12. if ($table) {
  13. $base = $project['path'] . $path . DIRECTORY_SEPARATOR . $table . '.php';
  14. if (is_file($base)) {
  15. $this->config = include $base;
  16. if (isset($this->config['partition']) && empty($partition)) {
  17. $partition = $this->config['partition'];
  18. }
  19. if (isset($this->config['store']) && $store == 'default') {
  20. $store = $this->config['store'];
  21. }
  22. $file = $app . DIRECTORY_SEPARATOR . $table . '.php';
  23. $this->config['app'] = $app;
  24. $this->config['table'] = DEVER_PROJECT . '_' . $app . '_' . $table;
  25. $this->config['load'] = $app . '/' . $table;
  26. $this->lang();
  27. } else {
  28. $this->config['table'] = $table;
  29. $this->config['struct'] = false;
  30. }
  31. }
  32. if ($partition) {
  33. $this->partition($partition);
  34. }
  35. $this->store = Dever::store($store, $this->partition);
  36. if (isset($file)) {
  37. $this->init($path, $store, $file);
  38. }
  39. Dever::reset();
  40. }
  41. public function partition($partition)
  42. {
  43. $setting = Dever::config('setting')['database']['partition'];
  44. if (is_array($partition)) {
  45. $this->partition = $partition;
  46. } elseif (is_string($partition)) {
  47. $e = '$v=' . $partition . ';';
  48. eval($e);
  49. $this->partition = $v;
  50. } else {
  51. $this->partition = $setting;
  52. }
  53. if ($this->partition) {
  54. foreach ($this->partition as $k => &$v) {
  55. $t = Dever::session($k);
  56. if ($t) {
  57. $v = $t;
  58. } elseif (($k == 'database' || $k == 'table') && strstr($v, '(')) {
  59. $e = '$v=' . $v . ';';
  60. eval($e);
  61. }
  62. }
  63. $this->partition['create'] = $setting['create'];
  64. }
  65. }
  66. private function lang()
  67. {
  68. if (isset($this->config['lang']) && isset($this->config['struct']) && $pack = Config::get('setting')['lang_pack']) {
  69. foreach ($this->config['lang'] as $lang) {
  70. if (isset($this->config['struct'][$lang])) {
  71. foreach ($pack as $key => $value) {
  72. if (Config::get('setting')['lang'] != $key) {
  73. $this->config['struct'][$key . '_' . $lang] = $this->config['struct'][$lang];
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. private function init($path, $store, $file)
  81. {
  82. $path .= DIRECTORY_SEPARATOR . $store;
  83. $data['index'] = $data['struct'] = 0;
  84. if ($this->partition) {
  85. if (isset($this->partition['database']) && $this->partition['database']) {
  86. if (!$this->partition['create']) {
  87. $this->config['table'] .= '_' . $this->partition['database'];
  88. }
  89. $path .= '/' . $this->partition['database'];
  90. }
  91. if (isset($this->partition['table']) && $this->partition['table']) {
  92. $this->config['table'] .= '_' . $this->partition['table'];
  93. $file = rtrim($file, '.php') . '_';
  94. $file .= $this->partition['table'] . '.php';
  95. }
  96. if (isset($this->partition['field']) && $this->partition['field']) {
  97. if (strstr($this->partition['field']['value'], 'date(')) {
  98. $this->partition['field']['type'] = 'time';
  99. $e = '$v=' . $this->partition['field']['value'] . ';';
  100. eval($e);
  101. $this->partition['field']['value'] = \Dever\Helper\Date::mktime($v);
  102. }
  103. $data['field'] = 0;
  104. }
  105. }
  106. $file = File::get($path . DIRECTORY_SEPARATOR . $file);
  107. if (is_file($file)) {
  108. $data = include $file;
  109. }
  110. foreach ($data as $k => $v) {
  111. if (isset($this->config[$k])) {
  112. $num = count($this->config[$k]);
  113. if ($v != $num) {
  114. $this->store->$k($this->config, $v);
  115. $data[$k] = $num;
  116. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  117. }
  118. } elseif ($k == 'field' && $v != $this->partition['field']['value']) {
  119. $this->store->partition($this->config, $this->partition['field']);
  120. $data['field'] = $this->partition['field']['value'];
  121. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  122. }
  123. }
  124. }
  125. public function load($param, $set = array(), $lock = false)
  126. {
  127. return $this->store->load($this->config['table'], $param, $set, $this->config['struct'], $lock);
  128. }
  129. public function select($param, $set = array(), $lock = false)
  130. {
  131. if (isset($this->partition['where']) && $this->partition['where']) {
  132. $param = array_merge($this->partition['where'], $param);
  133. }
  134. if (empty($set['order'])) {
  135. if (isset($this->config['order'])) {
  136. $set['order'] = $this->config['order'] . ',id desc';
  137. } else {
  138. $set['order'] = 'id desc';
  139. }
  140. }
  141. if (isset($set['num'])) {
  142. $set['limit'] = Paginator::init($set['num'], $set['page'] ?? 1, function()use($param){return $this->count($param);});
  143. }
  144. if ($lock && isset($this->config['type']) && $this->config['type'] == 'myisam') {
  145. $lock = false;
  146. }
  147. $result = $this->store->select($this->config['table'], $param, $set, $this->config['struct'], $lock);
  148. if (isset($set['num']) && empty($set['page'])) {
  149. Paginator::status(empty($result));
  150. }
  151. return $result;
  152. }
  153. public function find($param, $set = array(), $lock = false)
  154. {
  155. if (isset($this->partition['where']) && $this->partition['where']) {
  156. $param = array_merge($this->partition['where'], $param);
  157. }
  158. return $this->store->find($this->config['table'], $param, $set, $this->config['struct'], $lock);
  159. }
  160. public function count($param = array())
  161. {
  162. if (isset($this->partition['where']) && $this->partition['where']) {
  163. $param = array_merge($this->partition['where'], $param);
  164. }
  165. return $this->store->count($this->config['table'], $param, $this->config['struct']);
  166. }
  167. public function kv($param, $set = array())
  168. {
  169. $result = array();
  170. $data = $this->select($param, $set);
  171. if ($data) {
  172. if (empty($set['kv'])) {
  173. $set['kv'] = array('id', 'name');
  174. }
  175. foreach ($data as $k => $v) {
  176. $result[$v[$set['kv'][0]]] = $v[$set['kv'][1]];
  177. }
  178. }
  179. return $result;
  180. }
  181. public function up($param, $data, $lock = false)
  182. {
  183. $info = $this->find($param, array(), $lock);
  184. if ($info) {
  185. $state = $this->update($info['id'], $data);
  186. if ($state) {
  187. return $info['id'];
  188. }
  189. return false;
  190. } else {
  191. return $this->insert($data);
  192. }
  193. }
  194. public function insert($data)
  195. {
  196. if (empty($data['cdate'])) {
  197. $data['cdate'] = DEVER_TIME;
  198. }
  199. if (isset($this->partition['where']) && $this->partition['where']) {
  200. $data = array_merge($this->partition['where'], $data);
  201. }
  202. return $this->store->insert($this->config['table'], $data, $this->config['struct']);
  203. }
  204. public function update($param, $data, $lock = false)
  205. {
  206. if ($lock && isset($this->config['struct']['lock'])) {
  207. $info = $this->find($param, array('col' => 'id,lock'));
  208. if ($info) {
  209. $param['lock'] = $info['lock'];
  210. $data['lock'] = array('+', 1);
  211. } else {
  212. return false;
  213. }
  214. }
  215. return $this->store->update($this->config['table'], $param, $data, $this->config['struct']);
  216. }
  217. public function delete($param)
  218. {
  219. return $this->store->delete($this->config['table'], $param, $this->config['struct']);
  220. }
  221. public function begin()
  222. {
  223. return $this->store->begin();
  224. }
  225. public function commit()
  226. {
  227. return $this->store->commit();
  228. }
  229. public function rollback()
  230. {
  231. return $this->store->rollback();
  232. }
  233. public function query($sql, $bind = array(), $page = array())
  234. {
  235. if (strpos($sql, '{table}')) {
  236. $sql = str_replace('{table}', $this->config['table'], $sql);
  237. }
  238. if (isset($page['num'])) {
  239. if (strpos($sql, 'limit')) {
  240. $temp = explode('limit', $sql);
  241. $sql = $temp[0];
  242. }
  243. $sql .= ' limit ' . Paginator::init($page['num'], $page['page'] ?? 1, function()use($sql, $bind){return $this->queryCount($sql, $bind);});
  244. }
  245. $result = $this->store->query($sql, $bind);
  246. if (isset($page['num']) && empty($page['page'])) {
  247. $result = $result->fetchAll();
  248. Paginator::status(empty($result));
  249. }
  250. return $result;
  251. }
  252. public function queryCount($sql, $bind)
  253. {
  254. $sql = mb_strtolower($sql);
  255. if (strpos($sql, ' from ')) {
  256. $temp = explode(' from ', $sql);
  257. }
  258. if (isset($temp[1])) {
  259. if (strpos($temp[1], ' order ')) {
  260. $temp = explode(' order ', $temp[1]);
  261. $sql = $temp[0];
  262. } else {
  263. $sql = $temp[1];
  264. }
  265. if (strpos($sql, 'group')) {
  266. $sql = 'SELECT count(1) as num FROM (SELECT count(1) FROM '.$sql.' ) a ';
  267. } else {
  268. $sql = 'SELECT count(1) as num FROM ' . $sql;
  269. }
  270. return $this->store->query($sql, $bind)->fetchColumn();
  271. }
  272. }
  273. public function value($key, $value = false, $col = 'id,name')
  274. {
  275. if (isset($this->config['struct'][$key]) && $option = Dever::issets($this->config['struct'][$key], 'value')) {
  276. if (isset($this->config['struct'][$key]['option'])) {
  277. $option = $this->config['struct'][$key]['option'];
  278. } else {
  279. if (is_string($option) && strpos($option, 'Dever') === 0) {
  280. eval('$option=' . $option . ';');
  281. } elseif (is_string($option)) {
  282. $option = Dever::db($option)->select([], ['col' => $col]);
  283. } elseif (is_array($option) && !isset($option[0])) {
  284. $temp = $option;
  285. $option = array();
  286. $col = explode(',', $col);
  287. foreach ($temp as $k => $v) {
  288. $option[] = array($col[0] => $k, $col[1] => $v);
  289. }
  290. }
  291. $this->config['struct'][$key]['option'] = $option;
  292. }
  293. if ($value && $option) {
  294. if (is_array($value) && isset($value[$key])) {
  295. $value = $value[$key];
  296. }
  297. if (strpos($value, ',')) {
  298. $temp = explode(',', $value);
  299. $result = array();
  300. foreach ($temp as $v) {
  301. $state = Dever::in_array($option, $v);
  302. if ($state) {
  303. $result[] = $state;
  304. }
  305. }
  306. return implode('、', $result);
  307. }
  308. return Dever::in_array($option, $value);
  309. }
  310. return $option;
  311. }
  312. return false;
  313. }
  314. public function tree($where, $config, $func = false, $set = array())
  315. {
  316. $where[$config[0]] = $config[1];
  317. $data = $this->select($where, $set);
  318. if ($data) {
  319. foreach ($data as $k => &$v) {
  320. if ($func) $v = call_user_func($func, $k, $v);
  321. $config[1] = $v[$config[2]];
  322. $child = $this->tree($where, $config, $func, $set);
  323. if ($child) {
  324. $v['children'] = $child;
  325. }
  326. }
  327. }
  328. return $data;
  329. }
  330. public function show($where, $field = 'name', $str = '、')
  331. {
  332. $result = array();
  333. $data = $this->select($where);
  334. foreach ($data as $k => $v) {
  335. $result[] = $v[$field];
  336. }
  337. $result = implode($str, $result);
  338. return $result;
  339. }
  340. public function __call($method, $data)
  341. {
  342. if (isset($this->config['request'][$method])) {
  343. $method = $this->config['request'][$method];
  344. $param = array();
  345. if (isset($method['where'])) {
  346. $temp = array();
  347. foreach ($method['where'] as $k => $v) {
  348. if ($k == 'or' || $k == 'and') {
  349. foreach ($v as $k1 => $v1) {
  350. $this->callCreate($data, $temp, $k1, $v1);
  351. }
  352. } else {
  353. $this->callCreate($data, $temp, $k, $v);
  354. }
  355. }
  356. $param[] = $temp;
  357. }
  358. if (isset($method['data'])) {
  359. $temp = array();
  360. foreach ($method['data'] as $k => $v) {
  361. $this->callCreate($data, $temp, $k, $v);
  362. }
  363. $param[] = $temp;
  364. }
  365. if (isset($method['set']) && isset($data[1])) {
  366. $param[] = array_merge($method['set'], $data[1]);
  367. }
  368. $type = $method['type'];
  369. return $this->$type(...$param);
  370. }
  371. return false;
  372. }
  373. private function callCreate($data, &$param, $k, $v)
  374. {
  375. if (is_array($v)) {
  376. if (empty($data[0][$k])) {
  377. if (empty($v[2])) {
  378. return;
  379. }
  380. $data[0][$k] = $v[2];
  381. }
  382. $i = $v[0];
  383. $j = array($v[1], $data[0][$k]);
  384. } else {
  385. if (empty($data[0][$k])) {
  386. $data[0][$k] = $v;
  387. }
  388. $i = $k;
  389. $j = $data[0][$k];
  390. }
  391. $this->callCreateParam($param, $i, $j);
  392. }
  393. private function callCreateParam(&$param, $i, $j)
  394. {
  395. if (isset($param[$i])) {
  396. $i .= '#';
  397. return $this->callCreateParam($param, $i, $j);
  398. }
  399. $param[$i] = $j;
  400. }
  401. }