Model.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2016年11月6日
  7. * 应用开发继承模型类
  8. */
  9. namespace core\basic;
  10. use core\basic\Config;
  11. use core\view\Paging;
  12. use core\database\Mysqli;
  13. use core\database\Sqlite;
  14. use core\database\Pdo;
  15. class Model
  16. {
  17. // 数据库表名
  18. public $table;
  19. // 表主键
  20. public $pk = 'id';
  21. // 是否自动设置时间戳
  22. public $autoTimestamp = false;
  23. // 是否数字时间戳格式
  24. public $intTimeFormat = false;
  25. // 更新时间字段名
  26. public $updateTimeField = 'update_time';
  27. // 创建时间字段名
  28. public $createTimeField = 'create_time';
  29. // 程序执行的SQL语句记录
  30. private $exeSql = array();
  31. // 是否解密转义
  32. private $decode = false;
  33. // 查询语句构建
  34. private $sql = array();
  35. // 直接显示SQL语句
  36. private $showSql = false;
  37. // 直接显示结果
  38. private $showRs = false;
  39. // 数据库驱动对象
  40. private $dbDriver;
  41. // 查询语句
  42. private $selectSql = "SELECT %distinct% %field% FROM %table% %join% %where% %group% %having% %order% %union% %limit%";
  43. // 计数语句
  44. private $countSql = "SELECT COUNT(*) AS sum FROM %table% %join% %where% %group% %having% %union%";
  45. private $countSql2 = "SELECT %distinct% %field% FROM %table% %join% %where% %group% %having% %union%";
  46. // 插入语句
  47. private $insertSql = "INSERT INTO %table% %field% VALUES %value%";
  48. // 多条插入语句
  49. private $insertMultSql = "INSERT INTO %table% %field% %value%";
  50. // 复制插入语句
  51. private $insertFromSql = "INSERT INTO %table% %field% %from%";
  52. // 删除语句
  53. private $deleteSql = "DELETE FROM %table% %join% %where%";
  54. // 更新语句
  55. private $updateSql = "UPDATE %table% SET %value% %join% %where%";
  56. // 自动表名
  57. public function __construct()
  58. {
  59. if (! $this->table) {
  60. $table_name = Config::get('database.prefix') . hump_to_underline(str_replace('Model', '', basename(get_called_class())));
  61. $this->table = $table_name;
  62. }
  63. }
  64. // 对象方式动态调用数据库操作方法
  65. public function __call($methed, $args)
  66. {
  67. if (method_exists($this->getDb(), $methed)) {
  68. $result = call_user_func_array(array(
  69. $this->getDb(),
  70. $methed
  71. ), $args);
  72. return $result;
  73. } else {
  74. error('不存在数据库操作方法“' . $methed . '”,请核对再试!');
  75. }
  76. }
  77. // 获取数据库连接对象
  78. private function getDb()
  79. {
  80. if (! $this->dbDriver) {
  81. $type = Config::get('database.type');
  82. switch ($type) {
  83. case 'mysqli': // 使用mysqli连接数据库
  84. $this->dbDriver = Mysqli::getInstance();
  85. break;
  86. case 'sqlite': // 使用sqlite连接数据库
  87. $this->dbDriver = Sqlite::getInstance();
  88. break;
  89. default: // 默认使用PDO连接数据库
  90. $this->dbDriver = Pdo::getInstance();
  91. }
  92. }
  93. return $this->dbDriver;
  94. }
  95. // 执行SQL构造替换
  96. private function buildSql($sql, $clear = true)
  97. {
  98. preg_match_all('/\%([\w]+)\%/', $sql, $matches);
  99. foreach ($matches[1] as $key => $value) {
  100. if (isset($this->sql[$value]) && $this->sql[$value]) {
  101. $sql = str_replace("%$value%", $this->sql[$value], $sql);
  102. } else {
  103. if ($value == 'table') {
  104. $sql = str_replace("%$value%", $this->table, $sql);
  105. } else {
  106. $sql = str_replace("%$value%", '', $sql);
  107. }
  108. }
  109. }
  110. $this->exeSql[] = $sql;
  111. if ($clear) {
  112. $this->pk = 'id';
  113. $this->autoTimestamp = false;
  114. $this->intTimeFormat = false;
  115. $this->updateTimeField = 'update_time';
  116. $this->createTimeField = 'create_time';
  117. $this->sql = array();
  118. }
  119. if ($this->showSql && $clear) {
  120. exit($sql);
  121. } else {
  122. return $sql;
  123. }
  124. }
  125. /**
  126. * 关闭自动提交,开启事务模式(非连贯,直接调用)
  127. */
  128. final public function begin()
  129. {
  130. $this->getDb()->begin();
  131. }
  132. /**
  133. * 提交事务(非连贯,直接调用)
  134. *
  135. * @return \core\basic\Model
  136. */
  137. final public function commit()
  138. {
  139. $this->getDb()->commit();
  140. }
  141. /**
  142. * 内容输出
  143. *
  144. * @param mixed $data
  145. * @return mixed
  146. */
  147. final protected function outData($result)
  148. {
  149. if ($this->decode) {
  150. $result = decode_string($result);
  151. $this->decode = false;
  152. } else {
  153. $result = decode_slashes($result);
  154. }
  155. if ($this->showRs) {
  156. print_r($result);
  157. exit();
  158. } else {
  159. return $result;
  160. }
  161. }
  162. /**
  163. * 连贯操作:是否解码转义数据
  164. */
  165. final public function decode($flag = true)
  166. {
  167. if ($flag === true)
  168. $this->decode = true;
  169. return $this;
  170. }
  171. /**
  172. * 连贯操作:设置返回SQL语句,不真正执行,优先级高于showRS()
  173. *
  174. * @param string $flag
  175. * 调用默认为true
  176. * @return \core\basic\Model
  177. */
  178. final public function showSql($flag = true)
  179. {
  180. if ($flag === true)
  181. $this->showSql = true;
  182. return $this;
  183. }
  184. /**
  185. * 连贯操作:设置显示结果到页面
  186. *
  187. * @param string $flag
  188. * 调用默认为true
  189. * @return \core\basic\Model
  190. */
  191. final public function showRs($flag = true)
  192. {
  193. if ($flag === true)
  194. $this->showRs = true;
  195. return $this;
  196. }
  197. /**
  198. * 连贯操作:是否自动插入时间
  199. *
  200. * @param string $flag
  201. * @return \core\basic\Model
  202. */
  203. final public function autoTime($flag = true)
  204. {
  205. if ($flag === true)
  206. $this->sql['auto_time'] = true;
  207. return $this;
  208. }
  209. /**
  210. * 连贯操作:设置查询表全名
  211. *
  212. * @param mixed $table
  213. * 可以是字符串、数组,
  214. * 字符串:如"ay_user as a",
  215. * 如传递多个表:array('ay_user','ay_role'),
  216. * 传递多个表并设置别名:array('ay_user'=>'u','ay_role'=>'r')
  217. * @return \core\basic\Model
  218. */
  219. final public function table($table)
  220. {
  221. if (is_array($table)) {
  222. $table_string = '';
  223. foreach ($table as $key => $value) {
  224. if (is_int($key)) {
  225. $table_string .= '`' . $value . '`,';
  226. } else {
  227. $table_string .= '`' . $key . '` AS ' . $value . ',';
  228. }
  229. }
  230. $this->sql['table'] = substr($table_string, 0, - 1);
  231. } else {
  232. $this->sql['table'] = $table;
  233. }
  234. return $this;
  235. }
  236. /**
  237. * 连贯操作:设置查询表名
  238. *
  239. * @param mixed $table
  240. * 可以是字符串、数组,
  241. * 字符串:如"user as a",
  242. * 如传递多个表:array('user','role'),
  243. * 传递多个表并设置别名:array('user'=>'u','role'=>'r')
  244. * @return \core\basic\Model
  245. */
  246. final public function name($table)
  247. {
  248. $prefix = Config::get('database.prefix');
  249. if (is_array($table)) {
  250. $table_string = '';
  251. foreach ($table as $key => $value) {
  252. if (is_int($key)) {
  253. $table_string .= '`' . $prefix . $value . '`,';
  254. } else {
  255. $table_string .= '`' . $prefix . $key . '` AS ' . $value . ',';
  256. }
  257. }
  258. $this->table = substr($table_string, 0, - 1);
  259. } else {
  260. $this->table = $prefix . $table;
  261. }
  262. return $this;
  263. }
  264. /**
  265. * 连贯操作:设置数据库别名
  266. *
  267. * @param string $alias
  268. * 设置的别名名字,接收字符串,如:a
  269. * @return \core\basic\Model
  270. */
  271. final public function alias($alias)
  272. {
  273. if ($alias) {
  274. if (! isset($this->table))
  275. error('调用alias之前必须先设置table');
  276. if (strpos($this->table, ' AS ') === false) {
  277. $this->table = $this->table . ' AS ' . $alias;
  278. }
  279. }
  280. return $this;
  281. }
  282. /**
  283. * 连贯操作:设置返回唯一不同的值
  284. *
  285. * @param string $flag
  286. * 调用时默认为true,如果传递false则不使用
  287. * @return \core\basic\Model
  288. */
  289. final public function distinct($flag = true)
  290. {
  291. if ($flag === true)
  292. $this->sql['distinct'] = 'DISTINCT';
  293. return $this;
  294. }
  295. /**
  296. * 连贯操作:设置字段
  297. *
  298. * @param mixed $field
  299. * 可以为字符串、数组,
  300. * 如字符串:"name,password as pw",
  301. * 数组设置字段:array('name','password'),如果为非数字数组则设置别名,
  302. * 数组设置字段并设置别名:array('username'=>'name','password'=>'pw')
  303. * @return \core\basic\Model
  304. */
  305. final public function field($field)
  306. {
  307. if (is_array($field)) {
  308. $field_string = '';
  309. foreach ($field as $key => $value) {
  310. if (is_int($key)) {
  311. $field_string .= $value . ',';
  312. } else {
  313. $field_string .= $key . ' AS ' . $value . ',';
  314. }
  315. }
  316. $this->sql['field'] = substr($field_string, 0, - 1);
  317. } elseif ($field) {
  318. $this->sql['field'] = $field;
  319. }
  320. return $this;
  321. }
  322. /**
  323. * 连贯操作:设置查询条件
  324. *
  325. * @param mixed $where
  326. * 设置条件,可以为字符串、数组,
  327. * 字符串模式:如"id<1","name like %1",
  328. * 数组模式:array('username'=>'xie',"realname like '%谢%'")
  329. * @param string $connect
  330. * 调用本方法时与前面条件使用AND连接,$where参数数组内部的条件默认使用AND连接
  331. * @return \core\basic\Model
  332. */
  333. /**
  334. * 连贯操作:设置查询条件
  335. *
  336. * @param mixed $where
  337. * 设置条件,可以为字符串、数组,
  338. * 字符串模式:如"id<1","name like %1",
  339. * 数组模式:array('username'=>'xie',"realname like '%谢%'")
  340. * @param string $inConnect
  341. * 调用本方法时$where参数数组内部的条件默认使用AND连接
  342. * @param string $outConnect
  343. * 调用本方法时与前面条件使用AND连接
  344. * @param boolean $fuzzy
  345. * 条件是否为模糊匹配,即in匹配
  346. * @return \core\basic\Model
  347. */
  348. final public function where($where, $inConnect = 'AND', $outConnect = 'AND', $fuzzy = false)
  349. {
  350. if (! $where) {
  351. return $this;
  352. }
  353. if (isset($this->sql['where']) && $this->sql['where']) {
  354. $this->sql['where'] .= ' ' . $outConnect . '(';
  355. } else {
  356. $this->sql['where'] = 'WHERE(';
  357. }
  358. if (is_array($where)) {
  359. $where_string = '';
  360. $flag = false;
  361. foreach ($where as $key => $value) {
  362. if ($flag) { // 条件之间内部AND连接
  363. $where_string .= ' ' . $inConnect . ' ';
  364. } else {
  365. $flag = true;
  366. }
  367. if (! is_int($key)) {
  368. if ($fuzzy) {
  369. $where_string .= $key . " like '%" . $value . "%' ";
  370. } else {
  371. $where_string .= $key . "='" . $value . "' ";
  372. }
  373. } else {
  374. $where_string .= $value;
  375. }
  376. }
  377. $this->sql['where'] .= $where_string . ')';
  378. } else {
  379. $this->sql['where'] .= $where . ')';
  380. }
  381. return $this;
  382. }
  383. /**
  384. * 连贯操作:设置EXISTS查询
  385. *
  386. * @param string $subsql
  387. * 传递子查询
  388. * @return \core\basic\Model
  389. */
  390. final public function exists($subSql)
  391. {
  392. if (is_callable($subSql)) { // 闭包子查询
  393. $subSql = $subSql(new self());
  394. }
  395. if (isset($this->sql['where']) && $this->sql['where']) {
  396. $this->sql['where'] .= " AND EXISTS ($subSql)";
  397. } else {
  398. $this->sql['where'] = "WHERE EXISTS ($subSql)";
  399. }
  400. return $this;
  401. }
  402. /**
  403. * 连贯操作:设置NOT EXISTS查询
  404. *
  405. * @param string $subSql
  406. * 传递子查询
  407. * @return \core\basic\Model
  408. */
  409. final public function notExists($subSql)
  410. {
  411. if (is_callable($subSql)) { // 闭包子查询
  412. $subSql = $subSql(new self());
  413. }
  414. if (isset($this->sql['where']) && $this->sql['where']) {
  415. $this->sql['where'] .= " AND NOT EXISTS ($subSql)";
  416. } else {
  417. $this->sql['where'] = "WHERE NOT EXISTS ($subSql)";
  418. }
  419. return $this;
  420. }
  421. /**
  422. * 连贯操作:设置IN查询
  423. *
  424. * @param string $field
  425. * 传递需要比对的字段,如: 'username'
  426. * @param mixed $range
  427. * 字符串、数组、子查询,如:'1,2,3',array(1,2,3);
  428. * @return \core\basic\Model
  429. */
  430. final public function in($field, $range)
  431. {
  432. if (! $field)
  433. return $this;
  434. if (is_array($range)) {
  435. if (count($range) == 1) { // 单只有一个值时使用直接使用等于,提高读取性能
  436. $in_string = "$field='$range[0]'";
  437. } else {
  438. $in_string = "$field IN (" . implode_quot(',', $range) . ")";
  439. }
  440. } else {
  441. if (preg_match('/,/', $range)) {
  442. $in_string = "$field IN (" . implode_quot(',', explode(',', $range)) . ")";
  443. } else { // 传递单个字符串时直接相等处理
  444. $in_string = "$field = '$range'";
  445. }
  446. }
  447. if (isset($this->sql['where']) && $this->sql['where']) {
  448. $this->sql['where'] .= " AND $in_string";
  449. } else {
  450. $this->sql['where'] = "WHERE $in_string";
  451. }
  452. return $this;
  453. }
  454. /**
  455. * 连贯操作:设置NOT IN查询
  456. *
  457. * @param string $field
  458. * 传递需要比对的字段,如: id NOT IN (1,2,3)
  459. * @param mixed $range
  460. * 字符串、数组、子查询
  461. * @return \core\basic\Model
  462. */
  463. final public function notIn($field, $range)
  464. {
  465. if (! $field)
  466. return $this;
  467. if (is_array($range)) {
  468. $in_string = implode_quot(',', $range);
  469. } else {
  470. if (preg_match('/,/', $range)) {
  471. $in_string = implode_quot(',', explode(',', $range));
  472. } else {
  473. $in_string = "'$range'";
  474. }
  475. }
  476. if (isset($this->sql['where']) && $this->sql['where']) {
  477. $this->sql['where'] .= " AND $field NOT IN ($in_string)";
  478. } else {
  479. $this->sql['where'] = "WHERE $field NOT IN ($in_string)";
  480. }
  481. return $this;
  482. }
  483. /**
  484. * 连贯操作:设置关键字条件匹配
  485. *
  486. * @param string $field
  487. * 字段名,支持数组传递多个字段或多个字段用逗号隔开
  488. * @param string $keyword
  489. * 匹配关键字
  490. * @param string $matchType
  491. * 匹配模式,默认为all,可选left,right,equal
  492. * @return \core\database\Operate
  493. */
  494. final public function like($field, $keyword, $matchType = "all")
  495. {
  496. if (! $field)
  497. return $this;
  498. switch ($matchType) {
  499. case 'left':
  500. $keyword = "$keyword%";
  501. break;
  502. case 'right':
  503. $keyword = "%$keyword";
  504. break;
  505. case 'equal':
  506. case '==':
  507. $keyword = "$keyword";
  508. break;
  509. default:
  510. $keyword = "%$keyword%";
  511. }
  512. if (is_array($field) || preg_match('/,/', $field)) {
  513. if (! is_array($field)) {
  514. $field = explode(',', $field);
  515. }
  516. foreach ($field as $value) {
  517. if (isset($sqlStr)) {
  518. $sqlStr .= " OR $value LIKE '$keyword'";
  519. } else {
  520. $sqlStr = "$value LIKE '$keyword'";
  521. }
  522. }
  523. } else {
  524. $sqlStr = "$field LIKE '$keyword'";
  525. }
  526. if (isset($this->sql['where']) && $this->sql['where']) {
  527. $this->sql['where'] .= " AND ($sqlStr)";
  528. } else {
  529. $this->sql['where'] = "WHERE ($sqlStr)";
  530. }
  531. return $this;
  532. }
  533. /**
  534. * 连贯操作:设置关键字条件不匹配
  535. *
  536. * @param string $field
  537. * 字段名
  538. * @param string $keyword
  539. * 匹配关键字
  540. * @param string $matchType
  541. * 匹配模式,默认为all,可选left,right
  542. * @return \core\database\Operate
  543. */
  544. final public function notLike($field, $keyword, $matchType = "all")
  545. {
  546. if (! $field)
  547. return $this;
  548. switch ($matchType) {
  549. case 'left':
  550. $keyword = "$keyword%";
  551. break;
  552. case 'right':
  553. $keyword = "%$keyword";
  554. break;
  555. case 'equal':
  556. case '==':
  557. $keyword = "$keyword";
  558. break;
  559. default:
  560. $keyword = "%$keyword%";
  561. }
  562. if (is_array($field) || preg_match('/,/', $field)) {
  563. if (! is_array($field)) {
  564. $field = explode(',', $field);
  565. }
  566. foreach ($field as $value) {
  567. if (isset($sqlStr)) {
  568. $sqlStr .= " AND $value NOT LIKE '$keyword'";
  569. } else {
  570. $sqlStr = "$value NOT LIKE '$keyword'";
  571. }
  572. }
  573. } else {
  574. $sqlStr = "$field NOT LIKE '$keyword'";
  575. }
  576. if (isset($this->sql['where']) && $this->sql['where']) {
  577. $this->sql['where'] .= " AND ($sqlStr)";
  578. } else {
  579. $this->sql['where'] = "WHERE ($sqlStr)";
  580. }
  581. return $this;
  582. }
  583. /**
  584. * 连贯操作:设置查询排序
  585. *
  586. * @param mixed $order
  587. * 可以为字符串、数组,
  588. * 字符串模式:如"id DESC,name",
  589. * 数组模式:array('id'=>'DESC','name')
  590. * @return \core\basic\Model
  591. */
  592. final public function order($order)
  593. {
  594. if (is_array($order)) {
  595. $order_string = 'ORDER BY ';
  596. foreach ($order as $key => $value) {
  597. if (is_int($key)) {
  598. $order_string .= $value . ',';
  599. } else {
  600. $order_string .= $key . ' ' . $value . ',';
  601. }
  602. }
  603. $this->sql['order'] = substr($order_string, 0, - 1);
  604. } else {
  605. $this->sql['order'] = 'ORDER BY ' . $order;
  606. }
  607. return $this;
  608. }
  609. /**
  610. * 连贯操作:设置查询数量
  611. *
  612. * @param string $limit
  613. * 设置限制语句,可接受:
  614. * 单个参数数,如 1,条数
  615. * 单个参数字符串,如"1,10"
  616. * 两个参数数字,如:1,10
  617. * 注意:当使用了分页时会无效
  618. * @return \core\basic\Model
  619. */
  620. final public function limit($limit)
  621. {
  622. $var_num = func_num_args();
  623. if ($var_num > 1 || preg_match('/,/', $limit)) {
  624. if ($var_num > 1) {
  625. $var_arr = func_get_args();
  626. } else {
  627. $var_arr = explode(',', $limit);
  628. }
  629. switch (get_db_type()) {
  630. case 'mysql':
  631. $this->sql['limit'] = 'LIMIT ' . $var_arr[0] . ',' . $var_arr[1];
  632. break;
  633. case 'sqlite':
  634. $this->sql['limit'] = 'LIMIT ' . $var_arr[1] . ' OFFSET ' . $var_arr[0];
  635. break;
  636. case 'pgsql':
  637. $this->sql['limit'] = 'LIMIT ' . $var_arr[1] . ' OFFSET ' . $var_arr[0];
  638. break;
  639. }
  640. } else {
  641. $this->sql['limit'] = 'LIMIT ' . $limit;
  642. }
  643. return $this;
  644. }
  645. /**
  646. * 连贯操作:设置分组查询
  647. *
  648. * @param mixed $group
  649. * 可以传递字符串、数字数组,如"name,sex",array('name','sex')
  650. * @return \core\basic\Model
  651. */
  652. final public function group($group)
  653. {
  654. if (is_array($group)) {
  655. $group_string = 'GROUP BY ';
  656. foreach ($group as $key => $value) {
  657. $group_string .= $value . ',';
  658. }
  659. $this->sql['group'] = substr($group_string, 0, - 1);
  660. } else {
  661. $this->sql['group'] = 'GROUP BY ' . $group;
  662. }
  663. return $this;
  664. }
  665. /**
  666. * 连贯操作:设置查询条件having
  667. * 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。
  668. *
  669. * @param string $having
  670. * 传入字符串,需要完整having语句
  671. * @return \core\basic\Model
  672. */
  673. final public function having($having, $inConnect = 'AND', $outConnect = 'AND')
  674. {
  675. if (isset($this->sql['having']) && $this->sql['having']) {
  676. $this->sql['having'] .= ' ' . $outConnect . '(';
  677. } else {
  678. $this->sql['having'] = 'HAVING(';
  679. }
  680. if (is_array($having)) {
  681. $having_string = '';
  682. $flag = false;
  683. foreach ($having as $key => $value) {
  684. if ($flag) { // 条件之间内部AND连接
  685. $having_string .= ' ' . $inConnect . ' ';
  686. } else {
  687. $flag = true;
  688. }
  689. if (! is_int($key)) {
  690. $having_string .= $key . "='" . $value . "' ";
  691. } else {
  692. $having_string .= $value;
  693. }
  694. }
  695. $this->sql['having'] .= $having_string . ')';
  696. } else {
  697. $this->sql['having'] .= $having . ')';
  698. }
  699. return $this;
  700. }
  701. /**
  702. * 连贯操作:设置连接查询
  703. *
  704. * @param array $join
  705. * 可以为一维或二维数组,array('table','a.id=b.id','LEFT'),
  706. * array第一个参数为数据表,第二个参数为ON条件,第三个参数为类型,
  707. * 二维模式:array(
  708. * array('table b','a.id=b.aid','LEFT'),
  709. * array('table c','a.id=c.aid','LEFT')
  710. * )
  711. * @return \core\basic\Model
  712. */
  713. final public function join(array $join)
  714. {
  715. if (count($join) == count($join, 1)) {
  716. $join_string = '';
  717. if (isset($join[2])) {
  718. $join_string .= ' ' . $join[2] . ' JOIN ';
  719. } else {
  720. $join_string .= ' LEFT JOIN ';
  721. }
  722. $join_string .= $join[0] . ' ON ' . $join[1];
  723. if (isset($this->sql['join'])) {
  724. $this->sql['join'] .= $join_string;
  725. } else {
  726. $this->sql['join'] = $join_string;
  727. }
  728. } else {
  729. foreach ($join as $key => $value) {
  730. $this->join($value);
  731. }
  732. }
  733. return $this;
  734. }
  735. /**
  736. * 连贯操作:合并两个或多个 SELECT 语句的结果集
  737. *
  738. * @param array $subSql
  739. * 子查询可以使用数字数组传递多个
  740. * @param string $isAll
  741. * 是否UNION ALL
  742. * @return \core\basic\Model
  743. */
  744. final public function union($subSql, $isAll = false)
  745. {
  746. if (! isset($this->sql['union'])) {
  747. $this->sql['union'] = '';
  748. }
  749. if (is_callable($subSql)) { // 闭包子查询
  750. $subSql = $subSql(new self());
  751. }
  752. if (is_array($subSql)) {
  753. foreach ($subSql as $key => $value) {
  754. $this->union($value, $isAll);
  755. }
  756. } else {
  757. if ($isAll) {
  758. $this->sql['union'] .= " UNION ALL($subSql)";
  759. } else {
  760. $this->sql['union'] .= " UNION ($subSql)";
  761. }
  762. }
  763. return $this;
  764. }
  765. /**
  766. * 连贯操作:数据分页
  767. * 传递一个参数时为页数,传递两个参数时第二个为分页大小
  768. *
  769. * @return \core\basic\Model
  770. */
  771. final public function page($args = 1)
  772. {
  773. if ($args === false) {
  774. $this->sql['paging'] = false;
  775. return $this;
  776. }
  777. $this->sql['paging'] = true;
  778. $paging = Paging::getInstance();
  779. $var_num = func_num_args();
  780. if ($var_num > 1 || preg_match('/,/', $args)) {
  781. if ($var_num > 1) {
  782. $var_arr = func_get_args();
  783. } else {
  784. $var_arr = explode(',', $args);
  785. }
  786. $paging->page = $var_arr[0];
  787. $paging->pageSize = $var_arr[1];
  788. if (isset($var_arr[2])) {
  789. $paging->start = $var_arr[2];
  790. }
  791. } else {
  792. $paging->page = $args;
  793. }
  794. return $this;
  795. }
  796. /**
  797. * 连贯操作:待插入或更新数据数组,分解insert、update函数,实现 table($table)->data($data)->insert();
  798. *
  799. * @param array $data
  800. * @return \core\basic\Model
  801. */
  802. final public function data($data)
  803. {
  804. $this->sql['data'] = $data;
  805. return $this;
  806. }
  807. /**
  808. * 连贯操作:生成关联插入数据,如 用户对应多个角色,relation($ucode,$rcodes)
  809. *
  810. * @param string $field
  811. * 第一个字段的值,如用户编码
  812. * @param array $array
  813. * 第二个字段的值数组,如用户所属多个角色数组
  814. * @return \core\basic\Model
  815. */
  816. final public function relation($field, array $array)
  817. {
  818. if ($array) {
  819. foreach ($array as $value) {
  820. $data[] = array(
  821. $field,
  822. $value
  823. );
  824. }
  825. $this->sql['data'] = $data;
  826. }
  827. return $this;
  828. }
  829. /**
  830. * 连贯操作:用于从一个表复制信息到另一个表 ,实现INSERT INTO SELECT的功能
  831. *
  832. * @param string $subSql
  833. */
  834. final public function from($subSql)
  835. {
  836. if (is_callable($subSql)) { // 闭包子查询
  837. $subSql = $subSql(new self());
  838. }
  839. $this->sql['from'] = $subSql;
  840. return $this;
  841. }
  842. // ********************************数据查询************************************************************
  843. /**
  844. * 多条数据查询模型,select方法查询结果不存在,返回空数组
  845. *
  846. * @param string $type
  847. * 可选传递1,2,3返回不同格式数据数组
  848. * @return array
  849. */
  850. final public function select($type = null)
  851. {
  852. // 闭包查询
  853. if (is_callable($type)) {
  854. $type($this);
  855. return $this->select();
  856. }
  857. if (! isset($this->sql['field']) || ! $this->sql['field'])
  858. $this->sql['field'] = '*';
  859. // 如果调用了分页函数且分页,则执行分页处理
  860. if (isset($this->sql['paging']) && $this->sql['paging']) {
  861. if ($this->sql['group'] || $this->sql['distinct']) { // 解决使用分组时count(*)分页不准问题
  862. if (get_db_type() == 'mysql') {
  863. $this->limit(Paging::getInstance()->quikLimit()); // 分页
  864. $this->sql['field'] = 'SQL_CALC_FOUND_ROWS ' . $this->sql['field']; // 添加查询总记录
  865. $sql = $this->buildSql($this->selectSql);
  866. $result = $this->getDb()->all($sql, $type);
  867. $count_sql = "select FOUND_ROWS() as sum";
  868. if (! ! $rs = $this->getDb()->one($count_sql)) {
  869. $total = $rs->sum;
  870. // 分页内容
  871. $limit = Paging::getInstance()->limit($total, true); // 生成分页代码
  872. }
  873. } else {
  874. $count_sql = $this->buildSql($this->countSql2, false);
  875. // 获取记录总数
  876. if (! ! $rs = $this->getDb()->all($count_sql)) {
  877. $total = count($rs);
  878. // 分页内容
  879. $limit = Paging::getInstance()->limit($total, true);
  880. // 获取分页参数并设置分页
  881. $this->limit($limit);
  882. }
  883. }
  884. } else {
  885. // 生成总数计算语句
  886. $count_sql = $this->buildSql($this->countSql, false);
  887. // 获取记录总数
  888. if (! ! $rs = $this->getDb()->one($count_sql)) {
  889. $total = $rs->sum;
  890. // 分页内容
  891. $limit = Paging::getInstance()->limit($total, true);
  892. // 获取分页参数并设置分页
  893. $this->limit($limit);
  894. }
  895. }
  896. }
  897. // 构建查询语句
  898. $sql = $this->buildSql($this->selectSql);
  899. if ($type === false) {
  900. return $sql;
  901. }
  902. if (! isset($result)) {
  903. $result = $this->getDb()->all($sql, $type);
  904. }
  905. return $this->outData($result);
  906. }
  907. /**
  908. * 单条数据查询模型,find 方法查询结果不存在,返回 null
  909. *
  910. * @param string $type
  911. * 可选传递1,2,3返回不同格式数据数组
  912. * @return string|boolean|string
  913. */
  914. final public function find($type = null)
  915. {
  916. // 闭包查询
  917. if (is_callable($type)) {
  918. $type($this);
  919. return $this->find();
  920. }
  921. if (! isset($this->sql['field']))
  922. $this->sql['field'] = '*';
  923. $this->limit(1); // 强制查询一条
  924. $sql = $this->buildSql($this->selectSql); // 构建语句
  925. if ($type === false) {
  926. return $sql;
  927. }
  928. $result = $this->getDb()->one($sql, $type);
  929. return $this->outData($result);
  930. }
  931. /**
  932. * 返回指定字段数据数组
  933. *
  934. * @param string $name
  935. * 字段名字符串或数字数组,单个字段,返回一维数组,如果多个字段,返回二维数组数
  936. * @param string $key
  937. * 指定返回数组的键值字段
  938. * @return array
  939. */
  940. final public function column($fields, $key = null)
  941. {
  942. // 配置传递的字段
  943. if (is_array($fields)) {
  944. $fields = implode(',', $fields);
  945. }
  946. // 如果传递字段不含指定键则添加
  947. if ($key && ! preg_match('/(.*,|^)(' . $key . ')(,.*|$)/', $fields)) {
  948. $this->sql['field'] = $key . ',' . $fields;
  949. } else {
  950. $this->sql['field'] = $fields;
  951. }
  952. $sql = $this->buildSql($this->selectSql);
  953. $result = $this->getDb()->all($sql, 1);
  954. $data = array();
  955. foreach ($result as $vkey => $value) {
  956. if ($key) {
  957. $key_value = $value[$key];
  958. if (is_array($value) && count($value) == 2) {
  959. unset($value[$key]);
  960. $data[$key_value] = current($value);
  961. } else {
  962. $data[$key_value] = $value;
  963. }
  964. } else {
  965. if (is_array($value) && count($value) == 1) {
  966. $data[] = current($value);
  967. } else {
  968. $data[] = $value;
  969. }
  970. }
  971. }
  972. return $this->outData($data);
  973. }
  974. /**
  975. * 返回指定字段的一条数据的值模式
  976. *
  977. * @param string $name
  978. * 字段名
  979. * @return string 返回字段值
  980. */
  981. final public function value($field)
  982. {
  983. $this->sql['field'] = $field;
  984. $this->limit(1);
  985. $sql = $this->buildSql($this->selectSql);
  986. $result = $this->getDb()->one($sql, 2);
  987. if (isset($result[0])) {
  988. return $this->outData($result[0]);
  989. } else {
  990. return null;
  991. }
  992. }
  993. /**
  994. * 返回指定列最大值
  995. *
  996. * @param string $name
  997. * 字段名
  998. * @return boolean
  999. */
  1000. final public function max($field)
  1001. {
  1002. $this->sql['field'] = "MAX(`$field`)";
  1003. $sql = $this->buildSql($this->selectSql);
  1004. $result = $this->getDb()->one($sql, 2);
  1005. return $this->outData($result[0]);
  1006. }
  1007. /**
  1008. * 返回指定列最小值
  1009. *
  1010. * @param string $name
  1011. * 字段名
  1012. * @return boolean
  1013. */
  1014. final public function min($field)
  1015. {
  1016. $this->sql['field'] = "MIN(`$field`)";
  1017. $sql = $this->buildSql($this->selectSql);
  1018. $result = $this->getDb()->one($sql, 2);
  1019. return $this->outData($result[0]);
  1020. }
  1021. /**
  1022. * 返回指定列平均值
  1023. *
  1024. * @param string $name
  1025. * 字段名
  1026. * @return boolean
  1027. */
  1028. final public function avg($field)
  1029. {
  1030. $this->sql['field'] = "AVG(`$field`)";
  1031. $sql = $this->buildSql($this->selectSql);
  1032. $result = $this->getDb()->one($sql, 2);
  1033. return $this->outData($result[0]);
  1034. }
  1035. /**
  1036. * 返回指定列合计值
  1037. *
  1038. * @param string $name
  1039. * 字段名
  1040. * @return boolean
  1041. */
  1042. final public function sum($field)
  1043. {
  1044. $this->sql['field'] = "SUM(`$field`)";
  1045. $sql = $this->buildSql($this->selectSql);
  1046. $result = $this->getDb()->one($sql, 2);
  1047. if ($result[0]) {
  1048. return $this->outData($result[0]);
  1049. } else {
  1050. return 0;
  1051. }
  1052. }
  1053. // 返回统计数据
  1054. final public function count()
  1055. {
  1056. $this->sql['field'] = "COUNT(*)";
  1057. $sql = $this->buildSql($this->selectSql);
  1058. $result = $this->getDb()->one($sql, 2);
  1059. if ($result[0]) {
  1060. return $this->outData($result[0]);
  1061. } else {
  1062. return 0;
  1063. }
  1064. }
  1065. // ******************************数据插入*******************************************************
  1066. /**
  1067. * 数据插入模型
  1068. *
  1069. * @param array $data
  1070. * 可以为一维或二维数组,
  1071. * 一维数组:array('username'=>"xsh",'sex'=>'男'),
  1072. * 二维数组:array(
  1073. * array('username'=>"xsh",'sex'=>'男'),
  1074. * array('username'=>"gmx",'sex'=>'女')
  1075. * )
  1076. * @param boolean $batch
  1077. * 是否启用批量一次插入功能,默认true
  1078. * @return boolean|boolean|array
  1079. */
  1080. final public function insert(array $data = array(), $batch = true)
  1081. {
  1082. // 未传递数据时,使用data函数插入数据
  1083. if (! $data && isset($this->sql['data'])) {
  1084. return $this->insert($this->sql['data']);
  1085. }
  1086. if (is_array($data)) {
  1087. if (! $data)
  1088. return;
  1089. if (count($data) == count($data, 1)) { // 单条数据
  1090. $keys = '';
  1091. $values = '';
  1092. foreach ($data as $key => $value) {
  1093. $this->checkKey($key);
  1094. if (! is_numeric($key)) {
  1095. $keys .= "`" . $key . "`,";
  1096. $values .= "'" . $value . "',";
  1097. }
  1098. }
  1099. if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
  1100. $keys .= "`" . $this->createTimeField . "`,`" . $this->updateTimeField . "`,";
  1101. if ($this->intTimeFormat) {
  1102. $values .= "'" . time() . "','" . time() . "',";
  1103. } else {
  1104. $values .= "'" . date('Y-m-d H:i:s') . "','" . date('Y-m-d H:i:s') . "',";
  1105. }
  1106. }
  1107. if ($keys) { // 如果插入数据关联字段,则字段以关联数据为准,否则以设置字段为准
  1108. $this->sql['field'] = '(' . substr($keys, 0, - 1) . ')';
  1109. } elseif (isset($this->sql['field']) && $this->sql['field']) {
  1110. $this->sql['field'] = "({$this->sql['field']})";
  1111. }
  1112. $this->sql['value'] = "(" . substr($values, 0, - 1) . ")";
  1113. $sql = $this->buildSql($this->insertSql);
  1114. } else { // 多条数据
  1115. if ($batch) { // 批量一次性插入
  1116. $key_string = '';
  1117. $value_string = '';
  1118. $flag = false;
  1119. foreach ($data as $keys => $value) {
  1120. if (! $flag) {
  1121. $value_string .= ' SELECT ';
  1122. } else {
  1123. $value_string .= ' UNION All SELECT ';
  1124. }
  1125. foreach ($value as $key2 => $value2) {
  1126. // 字段获取只执行一次
  1127. if (! $flag && ! is_numeric($key2)) {
  1128. $this->checkKey($key2);
  1129. $key_string .= "`" . $key2 . "`,";
  1130. }
  1131. $value_string .= "'" . $value2 . "',";
  1132. }
  1133. $flag = true;
  1134. if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
  1135. if ($this->intTimeFormat) {
  1136. $value_string .= "'" . time() . "','" . time() . "',";
  1137. } else {
  1138. $value_string .= "'" . date('Y-m-d H:i:s') . "','" . date('Y-m-d H:i:s') . "',";
  1139. }
  1140. }
  1141. $value_string = substr($value_string, 0, - 1);
  1142. }
  1143. if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
  1144. $key_string .= "`" . $this->createTimeField . "`,`" . $this->updateTimeField . "`,";
  1145. }
  1146. if ($key_string) { // 如果插入数据关联字段,则字段以关联数据为准,否则以设置字段为准
  1147. $this->sql['field'] = '(' . substr($key_string, 0, - 1) . ')';
  1148. } elseif (isset($this->sql['field']) && $this->sql['field']) {
  1149. $this->sql['field'] = "({$this->sql['field']})";
  1150. }
  1151. $this->sql['value'] = $value_string;
  1152. $sql = $this->buildSql($this->insertMultSql);
  1153. // 判断SQL语句是否超过数据库设置
  1154. if (get_db_type() == 'mysql') {
  1155. $max_allowed_packet = $this->getDb()->one('SELECT @@global.max_allowed_packet', 2);
  1156. } else {
  1157. $max_allowed_packet = 1 * 1024 * 1024; // 其他类型数据库按照1M限制
  1158. }
  1159. if (strlen($sql) > $max_allowed_packet) { // 如果要插入的数据过大,则转换为一条条插入
  1160. return $this->insert($data, false);
  1161. }
  1162. } else { // 批量一条条插入
  1163. foreach ($data as $keys => $value) {
  1164. $result = $this->insert($value);
  1165. }
  1166. return $result;
  1167. }
  1168. }
  1169. } elseif ($this->sql['from']) {
  1170. if (isset($this->sql['field']) && $this->sql['field']) { // 表指定字段复制
  1171. $this->sql['field'] = "({$this->sql['field']})";
  1172. }
  1173. $sql = $this->buildSql($this->insertFromSql);
  1174. } else {
  1175. return;
  1176. }
  1177. $sql = preg_replace_r('/pboot:if/i', 'pboot@if', $sql); // 过滤插入cms条件语句
  1178. $sql = preg_replace_r('/pboot:sql/i', 'pboot@sql', $sql); // 过滤插入cms条件语句
  1179. return $this->getDb()->amd($sql);
  1180. }
  1181. /**
  1182. * 插入数据并返回自增ID值
  1183. *
  1184. * @param array $data
  1185. * 可以为一维或二维数组
  1186. * @param boolean $batch
  1187. * 是否启用批量一次插入功能,默认true
  1188. * @return number|string|boolean
  1189. */
  1190. final public function insertGetId(array $data = null, $batch = true)
  1191. {
  1192. if ($this->insert($data, $batch)) {
  1193. return $this->getDb()->insertId();
  1194. } else {
  1195. return false;
  1196. }
  1197. }
  1198. // ******************************数据更新*******************************************************
  1199. /**
  1200. * 数据更新
  1201. *
  1202. * @param array $data
  1203. * 传入字符串、数组
  1204. * 字符串如:"username='xsh'"
  1205. * 数组如:array('username'=>'test','sex'=>'女')
  1206. * @return void|boolean|boolean|array
  1207. */
  1208. final public function update($data = null)
  1209. {
  1210. // 未传递数据时使用data函数插入数据
  1211. if (! $data && $this->sql['data']) {
  1212. return $this->update($this->sql['data']);
  1213. }
  1214. $update_string = '';
  1215. if (is_array($data)) {
  1216. if (! $data)
  1217. return;
  1218. foreach ($data as $key => $value) {
  1219. $this->checkKey($key);
  1220. $temp_v_start = substr($value, 0, 2);
  1221. $temp_v_end = substr($value, 2);
  1222. if (is_numeric($temp_v_end) && $temp_v_start == "-=") {
  1223. $update_string .= "`$key`= $key-$temp_v_end,"; // 自减
  1224. } elseif (is_numeric($temp_v_end) && $temp_v_start == "+=") {
  1225. $update_string .= "`$key`= $key+$temp_v_end,"; // 自加
  1226. } else {
  1227. $update_string .= "`$key`='$value',";
  1228. }
  1229. }
  1230. $update_string = substr($update_string, 0, - 1);
  1231. } else {
  1232. $update_string = $data;
  1233. }
  1234. if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
  1235. if ($this->intTimeFormat) {
  1236. $update_string .= ",`" . $this->updateTimeField . "`=' " . time() . "'";
  1237. } else {
  1238. $update_string .= ",`" . $this->updateTimeField . "`=' " . date('Y-m-d H:i:s') . "'";
  1239. }
  1240. }
  1241. $this->sql['value'] = $update_string;
  1242. $sql = $this->buildSql($this->updateSql);
  1243. $sql = preg_replace_r('/pboot:if/i', 'pboot@if', $sql); // 过滤插入cms条件语句
  1244. $sql = preg_replace_r('/pboot:sql/i', 'pboot@sql', $sql); // 过滤插入cms条件语句
  1245. return $this->getDb()->amd($sql);
  1246. }
  1247. /**
  1248. * 更新指定字段
  1249. *
  1250. * @param string $field
  1251. * 字段名
  1252. * @param string $value
  1253. * 值
  1254. * @return boolean|boolean|array
  1255. */
  1256. final public function setField($field, $value)
  1257. {
  1258. $this->checkKey($field);
  1259. $this->sql['value'] = "`$field`='$value'";
  1260. if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
  1261. if ($this->intTimeFormat) {
  1262. $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . time() . "'";
  1263. } else {
  1264. $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . date('Y-m-d H:i:s') . "'";
  1265. }
  1266. }
  1267. $sql = $this->buildSql($this->updateSql);
  1268. return $this->getDb()->amd($sql);
  1269. }
  1270. /**
  1271. * 字段自增
  1272. *
  1273. * @param string $field
  1274. * 字段
  1275. * @param number $value
  1276. * 值
  1277. * @return boolean|boolean|array
  1278. */
  1279. final public function setInc($field, $value = 1)
  1280. {
  1281. $this->checkKey($field);
  1282. $this->sql['value'] = " `$field`= $field+$value";
  1283. if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
  1284. if ($this->intTimeFormat) {
  1285. $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . time() . "'";
  1286. } else {
  1287. $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . date('Y-m-d H:i:s') . "'";
  1288. }
  1289. }
  1290. $sql = $this->buildSql($this->updateSql);
  1291. return $this->getDb()->amd($sql);
  1292. }
  1293. /**
  1294. * 字段自减
  1295. *
  1296. * @param string $field
  1297. * 字段
  1298. * @param number $value
  1299. * 值
  1300. * @return boolean|boolean|array
  1301. */
  1302. final public function setDec($field, $value = 1)
  1303. {
  1304. $this->checkKey($field);
  1305. $this->sql['value'] = " `$field`= $field-$value";
  1306. if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
  1307. if ($this->intTimeFormat) {
  1308. $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . time() . "'";
  1309. } else {
  1310. $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . date('Y-m-d H:i:s') . "'";
  1311. }
  1312. }
  1313. $sql = $this->buildSql($this->updateSql);
  1314. return $this->getDb()->amd($sql);
  1315. }
  1316. // ***************************数据删除*******************************************************
  1317. /**
  1318. * 数据删除
  1319. *
  1320. * @param mixed $data
  1321. * 可以为字符串或数组,如"1,2,3",array(1,2,3)
  1322. * @param string $key
  1323. * 字段名,默认为id
  1324. * @return boolean|boolean|array
  1325. */
  1326. final public function delete($data = null, $key = null)
  1327. {
  1328. if ($data) {
  1329. if (! $key)
  1330. $key = $this->pk;
  1331. $this->checkKey($key);
  1332. if (is_array($data) || preg_match('/,/', $data)) {
  1333. $this->in($key, $data);
  1334. } else {
  1335. $this->where("$key='$data'");
  1336. }
  1337. }
  1338. $sql = $this->buildSql($this->deleteSql);
  1339. return $this->getDb()->amd($sql);
  1340. }
  1341. // 检测key值
  1342. private function checkKey($key)
  1343. {
  1344. if (! $key)
  1345. return;
  1346. if (! preg_match('/^[\w\.\-]+$/', $key)) {
  1347. error('传递的SQL数据中含有非法字符:' . $key);
  1348. }
  1349. }
  1350. }