Model.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php namespace Dever;
  2. class Model
  3. {
  4. protected $method = '';
  5. protected $store;
  6. protected $config = array();
  7. public function __construct($table, $app, $path)
  8. {
  9. $store = 'default';
  10. if (strpos($table, '.')) {
  11. list($table, $this->method) = explode('.', $table);
  12. }
  13. if (strpos($table, ':')) {
  14. list($table, $store) = explode(':', $table);
  15. }
  16. $project = Project::load($app);
  17. $file = $path . DIRECTORY_SEPARATOR . $table . '.php';
  18. $base = $project['path'] . $file;
  19. if (is_file($base)) {
  20. $this->config = include $base;
  21. }
  22. $this->config['name'] = DEVER_PROJECT . '_' . $app . '_' . $table;
  23. $this->config['app'] = $app;
  24. if (isset($this->config['store'])) {
  25. $store = $this->config['store'];
  26. }
  27. $setting = Config::get('setting')['database'][$store];
  28. $class = 'Dever\\Store\\' . $setting['type'];
  29. $this->store = $class::getInstance($store, $setting);
  30. $this->load();
  31. $this->init($file);
  32. }
  33. private function load()
  34. {
  35. if (isset($this->config['lang']) && isset($this->config['struct']) && $pack = Config::get('setting')['lang_pack']) {
  36. foreach ($this->config['lang'] as $lang) {
  37. if (isset($this->config['struct'][$lang])) {
  38. foreach ($pack as $key => $value) {
  39. if (Config::get('setting')['lang'] != $key) {
  40. $this->config['struct'][$key . '_' . $lang] = $this->config['struct'][$lang];
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. private function init($file)
  48. {
  49. $data['struct'] = $data['index'] = 0;
  50. $file = Path::get($file);
  51. if (is_file($file)) {
  52. $data = include $file;
  53. }
  54. foreach ($data as $k => $v) {
  55. if (isset($this->config[$k])) {
  56. $num = count($this->config[$k]);
  57. if ($v != $num) {
  58. $this->store->$k($this->config, $v);
  59. $data[$k] = $num;
  60. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  61. }
  62. }
  63. }
  64. }
  65. public function select($param = array(), $set, $call = false)
  66. {
  67. return $this->store->select($this->config['name'], $param, $set);
  68. if ($call) {
  69. }
  70. if ($data) {
  71. if (isset($set['reset'])) {
  72. $result = $this->reset($data, $set['reset'], $call);
  73. } else {
  74. if ($call) {
  75. foreach ($data as $row) {
  76. $result[] = $call($row);
  77. }
  78. }
  79. }
  80. }
  81. return $result;
  82. }
  83. public function find($where, $call = false)
  84. {
  85. return $this->db()->find($where, $call);
  86. }
  87. public function query($sql, $data = array(), $method = '')
  88. {
  89. return $this->db()->exe($sql, $data, $method);
  90. }
  91. public function fetch($sql, $data = array(), $cache = false)
  92. {
  93. return $this->fetchAll($sql, $data, false, $cache, 'fetch');
  94. }
  95. public function fetchAll($sql, $data = array(), $page = array(), $cache = false, $method = 'fetchAll')
  96. {
  97. $this->setTable($sql);
  98. if (!$cache && !empty(Config::get('cache')->mysql)) {
  99. $cache = 'getSql_' . md5($sql . serialize($data));
  100. if ($page && $p = Input::get('page')) {
  101. $cache .= '_p' . $p;
  102. }
  103. }
  104. if ($cache) {
  105. $result = $this->db()->cache($cache);
  106. if ($result) {
  107. return $result;
  108. }
  109. }
  110. if ($page) {
  111. $result = $this->page($page, $sql, $data);
  112. } else {
  113. $result = $this->query($sql, $data)->$method();
  114. }
  115. if ($cache) {
  116. $this->db()->cache($cache, $result);
  117. } else {
  118. $this->db()->log($this->sql, $data, $result);
  119. }
  120. return $result;
  121. }
  122. public function rowCount($sql, $data = array())
  123. {
  124. return $this->query($sql, $data)->rowcount();
  125. }
  126. public function row($sql, $data = array())
  127. {
  128. return $this->query($sql, $data)->rowcount();
  129. }
  130. public function lastId($sql, $data = array())
  131. {
  132. return $this->query($sql, $data)->id();
  133. }
  134. public function page($config = array(), $sql = false, $data = array())
  135. {
  136. return $this->db()->getPageBySql($config, $sql, $data, $this);
  137. }
  138. public function index($index)
  139. {
  140. return $this->db()->index($index);
  141. }
  142. public function begin()
  143. {
  144. return $this->db()->begin();
  145. }
  146. public function commit()
  147. {
  148. return $this->db()->commit();
  149. }
  150. public function rollback()
  151. {
  152. return $this->db()->rollback();
  153. }
  154. /**
  155. * __call
  156. *
  157. * @return mixd
  158. */
  159. public function __call($method, $param)
  160. {
  161. if (isset($param[0][0]) && is_array($param[0][0])) {
  162. $result = array();
  163. foreach ($param[0] as $k => $v) {
  164. $result[] = $this->$method($v);
  165. }
  166. return $result;
  167. }
  168. $call = false;
  169. if (isset($param[1])) {
  170. if (is_object($param[1])) {
  171. $call = $param[1];
  172. } elseif ($method == 'insert') {
  173. $param[0]['insert_value_num'] = $param[1];
  174. }
  175. }
  176. $param = $param ? $this->initParam($param[0], $method) : array();
  177. $key = $this->table . $method . md5(serialize($param));
  178. if (isset($param['clear']) && $param['clear'] && isset($this->data[$key])) {
  179. unset($this->data[$key]);
  180. }
  181. $this->compatible($key, $method, $param);
  182. if (!isset($this->data[$key])) {
  183. $this->request($method, $param);
  184. if (isset($this->config['request'][$method]['type']) && in_array($this->config['request'][$method]['type'], array('update', 'insert'))) {
  185. return $this->callData($method, $param, $call);
  186. }
  187. $this->data[$key] = $this->callData($method, $param, $call);
  188. }
  189. return $this->data[$key];
  190. }
  191. /**
  192. * getData
  193. *
  194. * @return mixd
  195. */
  196. private function callData($method, $param, $call = false)
  197. {
  198. $param = $this->search($method, $param);
  199. $handle = new Model\Handle($method, $this->config, $param, $call);
  200. return $handle->get();
  201. }
  202. /**
  203. * compatible
  204. *
  205. * @return mixd
  206. */
  207. protected function compatible($key, $method, $param)
  208. {
  209. if (Config::get('database')->compatible && isset($this->config['project'])) {
  210. $file = $this->config['project']['path'] . 'database/' . ucfirst(Config::get('database')->compatible) . '/' . ucfirst($this->config['name']) . '.php';
  211. if (is_file($file)) {
  212. $class = ucfirst($this->config['project']['name']) . '\\Database\\' . ucfirst(Config::get('database')->compatible) . '\\' . ucfirst($this->config['name']);
  213. if (class_exists($class) && method_exists($class, $method)) {
  214. $this->data[$key] = $class::$method($param);
  215. }
  216. }
  217. }
  218. }
  219. /**
  220. * request
  221. *
  222. * @return mixd
  223. */
  224. protected function request($method, $param)
  225. {
  226. if (!isset($this->config['struct'])) {
  227. $this->config['struct'] = array();
  228. }
  229. if (empty($this->config['request'][$method]) && isset($this->config['struct'])) {
  230. $search = '';
  231. if (isset($param['search_type'])) {
  232. $search = $param['search_type'];
  233. }
  234. $this->config['request'][$method] = Model\Request::get($this->table, $method, $this->config['struct'], $search);
  235. if ($method == 'list' && isset($this->config['manage']['list_type']) && $this->config['manage']['list_type']) {
  236. $this->config['request']['list']['page'][0] = 50000;
  237. }
  238. if (isset($this->config['request'][$method]['type']) && isset($this->config['request'][$method]['option']) && $this->config['request'][$method]['type'] == 'all' && isset($this->config['request'][$method . '_option'])) {
  239. $this->config['request'][$method]['option'] = array_merge($this->config['request'][$method]['option'], $this->config['request'][$method . '_option']);
  240. }
  241. }
  242. if (isset($param['config']) && isset($this->config['request'][$method]['config']) && $this->config['request'][$method]['config']) {
  243. $this->config['request'][$method] = array_merge($this->config['request'][$method], $param['config']);
  244. unset($param['config']);
  245. unset($this->config['request'][$method]['config']);
  246. }
  247. if (in_array($method, array('all', 'state', 'total', 'list', 'find')) && isset($param['option'])) {
  248. foreach ($param['option'] as $k => $v) {
  249. $this->config['request'][$method]['option'][$k] = $v;
  250. }
  251. }
  252. if ($method == 'list') {
  253. $search = Dever::search_button();
  254. if ($search) {
  255. $this->config['request']['list']['group'] = $search[0];
  256. $this->config['request']['list']['col'] = $search[1];
  257. }
  258. }
  259. if (isset($param['join'])) {
  260. $this->config['request'][$method]['join'] = $param['join'];
  261. if (isset($this->config['request'][$method]['option'])) {
  262. foreach ($this->config['request'][$method]['option'] as $k => $v) {
  263. if (is_string($v) && !strstr($v, 't_1') && !strstr($k, '-')) {
  264. $this->config['request'][$method]['option'][$k] = 'yes-t_1.' . $k;
  265. }
  266. if (is_array($v) && !strstr($v[0], 't_1')) {
  267. if (strstr($v[0], '-')) {
  268. $temp = explode('-', $v[0]);
  269. $this->config['request'][$method]['option'][$k][0] = 'yes-t_1.' . $temp[1];
  270. } else {
  271. $this->config['request'][$method]['option'][$k][0] = 'yes-t_1.' . $k;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. if (DEVER_APP_NAME != 'manage') {
  278. $lang = Dever::langConfig();
  279. if (Dever::config('base')->lang && $lang && $lang != Dever::config('base')->lang && isset($this->config['manage']['lang']) && $this->config['manage']['lang']) {
  280. $col = array();
  281. foreach ($this->config['manage']['lang'] as $k => $v) {
  282. $col[] = $lang . '_' . $v . ' as `' . $v . '`';
  283. }
  284. if (!isset($this->config['request'][$method]['col'])) {
  285. $this->config['request'][$method]['col'] = '*';
  286. }
  287. $this->config['request'][$method]['col'] .= ',' . implode(',', $col);
  288. }
  289. }
  290. }
  291. /**
  292. * search
  293. *
  294. * @return mixd
  295. */
  296. protected function search($method, $param)
  297. {
  298. if (isset($param['search_type']) && $param['search_type'] == 2) {
  299. $join = array();
  300. $i = 2;
  301. foreach ($param as $k => $v) {
  302. if (strpos($k, '-') !== false) {
  303. $k = str_replace(array('option_', 'where_'), '', $k);
  304. if (isset($this->config['struct'][$k]) && isset($this->config['struct'][$k]['sync'])) {
  305. $temp = explode('-', $k);
  306. $join[] = array
  307. (
  308. 'table' => $temp[0] . '/' . $temp[1],
  309. 'type' => 'left join',
  310. 'on' => $this->config['struct'][$k]['sync'],
  311. );
  312. $t = 't_' . $i . '.' . $temp[2];
  313. $this->config['request'][$method]['option'][$t] = $this->config['request'][$method]['option'][$k];
  314. $param['option_' . $t] = $param['option_' . $k];
  315. unset($this->config['request'][$method]['option'][$k]);
  316. unset($param['option_' . $k]);
  317. $i++;
  318. }
  319. }
  320. }
  321. if ($join) {
  322. $this->config['request'][$method]['join'] = $join;
  323. }
  324. }
  325. return $param;
  326. }
  327. protected function searchFulltext($k, $param)
  328. {
  329. }
  330. /**
  331. * initParam
  332. *
  333. * @return mixd
  334. */
  335. private function initParam($param, $method)
  336. {
  337. if ($param && !is_array($param)) {
  338. if ($this->config['link']) {
  339. $param = array('id' => $param);
  340. } else {
  341. $param = array('where_id' => $param, 'option_id' => $param);
  342. }
  343. }
  344. return $param;
  345. }
  346. }