Model.php 17 KB

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