Model.class.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think;
  12. /**
  13. * ThinkPHP Model模型类
  14. * 实现了ORM和ActiveRecords模式
  15. */
  16. class Model
  17. {
  18. // 当前数据库操作对象
  19. protected $db = null;
  20. // 数据库对象池
  21. private $_db = array();
  22. // 主键名称
  23. protected $pk = 'id';
  24. // 主键是否自动增长
  25. protected $autoinc = false;
  26. // 数据表前缀
  27. protected $tablePrefix = null;
  28. // 模型名称
  29. protected $name = '';
  30. // 数据库名称
  31. protected $dbName = '';
  32. //数据库配置
  33. protected $connection = '';
  34. // 数据表名(不包含表前缀)
  35. protected $tableName = '';
  36. // 实际数据表名(包含表前缀)
  37. protected $trueTableName = '';
  38. // 最近错误信息
  39. protected $error = '';
  40. // 字段信息
  41. protected $fields = array();
  42. // 数据信息
  43. protected $data = array();
  44. // 查询表达式参数
  45. protected $options = array();
  46. protected $_validate = array(); // 自动验证定义
  47. protected $_auto = array(); // 自动完成定义
  48. protected $_map = array(); // 字段映射定义
  49. protected $_scope = array(); // 命名范围定义
  50. // 是否自动检测数据表字段信息
  51. protected $autoCheckFields = true;
  52. // 是否批处理验证
  53. protected $patchValidate = false;
  54. // 链操作方法列表
  55. protected $methods = array('strict', 'order', 'alias', 'having', 'group', 'lock', 'distinct', 'auto', 'filter', 'validate', 'result', 'token', 'index', 'force');
  56. /**
  57. * 架构函数
  58. * 取得DB类的实例对象 字段检查
  59. * @access public
  60. * @param string $name 模型名称
  61. * @param string $tablePrefix 表前缀
  62. * @param mixed $connection 数据库连接信息
  63. */
  64. public function __construct($name = '', $tablePrefix = '', $connection = '')
  65. {
  66. // 模型初始化
  67. $this->_initialize();
  68. // 获取模型名称
  69. if (!empty($name)) {
  70. if (strpos($name, '.')) {
  71. // 支持 数据库名.模型名的 定义
  72. list($this->dbName, $this->name) = explode('.', $name);
  73. } else {
  74. $this->name = $name;
  75. }
  76. } elseif (empty($this->name)) {
  77. $this->name = $this->getModelName();
  78. }
  79. // 设置表前缀
  80. if (is_null($tablePrefix)) {
  81. // 前缀为Null表示没有前缀
  82. $this->tablePrefix = '';
  83. } elseif ('' != $tablePrefix) {
  84. $this->tablePrefix = $tablePrefix;
  85. } elseif (!isset($this->tablePrefix)) {
  86. $this->tablePrefix = C('DB_PREFIX');
  87. }
  88. // 数据库初始化操作
  89. // 获取数据库操作对象
  90. // 当前模型有独立的数据库连接信息
  91. $this->db(0, empty($this->connection) ? $connection : $this->connection, true);
  92. }
  93. /**
  94. * 自动检测数据表信息
  95. * @access protected
  96. * @return void
  97. */
  98. protected function _checkTableInfo()
  99. {
  100. // 如果不是Model类 自动记录数据表信息
  101. // 只在第一次执行记录
  102. if (empty($this->fields)) {
  103. // 如果数据表字段没有定义则自动获取
  104. if (C('DB_FIELDS_CACHE')) {
  105. $db = $this->dbName ?: C('DB_NAME');
  106. $fields = F('_fields/' . strtolower($db . '.' . $this->tablePrefix . $this->name));
  107. if ($fields) {
  108. $this->fields = $fields;
  109. if (!empty($fields['_pk'])) {
  110. $this->pk = $fields['_pk'];
  111. }
  112. return;
  113. }
  114. }
  115. // 每次都会读取数据表信息
  116. $this->flush();
  117. }
  118. }
  119. /**
  120. * 获取字段信息并缓存
  121. * @access public
  122. * @return void
  123. */
  124. public function flush()
  125. {
  126. // 缓存不存在则查询数据表信息
  127. $this->db->setModel($this->name);
  128. $fields = $this->db->getFields($this->getTableName());
  129. if (!$fields) {
  130. // 无法获取字段信息
  131. return false;
  132. }
  133. $this->fields = array_keys($fields);
  134. unset($this->fields['_pk']);
  135. foreach ($fields as $key => $val) {
  136. // 记录字段类型
  137. $type[$key] = $val['type'];
  138. if ($val['primary']) {
  139. // 增加复合主键支持
  140. if (isset($this->fields['_pk']) && null != $this->fields['_pk']) {
  141. if (is_string($this->fields['_pk'])) {
  142. $this->pk = array($this->fields['_pk']);
  143. $this->fields['_pk'] = $this->pk;
  144. }
  145. $this->pk[] = $key;
  146. $this->fields['_pk'][] = $key;
  147. } else {
  148. $this->pk = $key;
  149. $this->fields['_pk'] = $key;
  150. }
  151. if ($val['autoinc']) {
  152. $this->autoinc = true;
  153. }
  154. }
  155. }
  156. // 记录字段类型信息
  157. $this->fields['_type'] = $type;
  158. // 2008-3-7 增加缓存开关控制
  159. if (C('DB_FIELDS_CACHE')) {
  160. // 永久缓存数据表信息
  161. $db = $this->dbName ?: C('DB_NAME');
  162. F('_fields/' . strtolower($db . '.' . $this->tablePrefix . $this->name), $this->fields);
  163. }
  164. }
  165. /**
  166. * 设置数据对象的值
  167. * @access public
  168. * @param string $name 名称
  169. * @param mixed $value 值
  170. * @return void
  171. */
  172. public function __set($name, $value)
  173. {
  174. // 设置数据对象属性
  175. $this->data[$name] = $value;
  176. }
  177. /**
  178. * 获取数据对象的值
  179. * @access public
  180. * @param string $name 名称
  181. * @return mixed
  182. */
  183. public function __get($name)
  184. {
  185. return isset($this->data[$name]) ? $this->data[$name] : null;
  186. }
  187. /**
  188. * 检测数据对象的值
  189. * @access public
  190. * @param string $name 名称
  191. * @return boolean
  192. */
  193. public function __isset($name)
  194. {
  195. return isset($this->data[$name]);
  196. }
  197. /**
  198. * 销毁数据对象的值
  199. * @access public
  200. * @param string $name 名称
  201. * @return void
  202. */
  203. public function __unset($name)
  204. {
  205. unset($this->data[$name]);
  206. }
  207. /**
  208. * 利用__call方法实现一些特殊的Model方法
  209. * @access public
  210. * @param string $method 方法名称
  211. * @param array $args 调用参数
  212. * @return mixed
  213. */
  214. public function __call($method, $args)
  215. {
  216. if (in_array(strtolower($method), $this->methods, true)) {
  217. // 连贯操作的实现
  218. $this->options[strtolower($method)] = $args[0];
  219. return $this;
  220. } elseif (in_array(strtolower($method), array('count', 'sum', 'min', 'max', 'avg'), true)) {
  221. // 统计查询的实现
  222. $field = isset($args[0]) ? $args[0] : '*';
  223. return $this->getField(strtoupper($method) . '(' . $field . ') AS tp_' . $method);
  224. } elseif (strtolower(substr($method, 0, 5)) == 'getby') {
  225. // 根据某个字段获取记录
  226. $field = parse_name(substr($method, 5));
  227. $where[$field] = $args[0];
  228. return $this->where($where)->find();
  229. } elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') {
  230. // 根据某个字段获取记录的某个值
  231. $name = parse_name(substr($method, 10));
  232. $where[$name] = $args[0];
  233. return $this->where($where)->getField($args[1]);
  234. } elseif (isset($this->_scope[$method])) {
  235. // 命名范围的单独调用支持
  236. return $this->scope($method, $args[0]);
  237. } else {
  238. E(__CLASS__ . ':' . $method . L('_METHOD_NOT_EXIST_'));
  239. return;
  240. }
  241. }
  242. // 回调方法 初始化模型
  243. protected function _initialize()
  244. {}
  245. /**
  246. * 对保存到数据库的数据进行处理
  247. * @access protected
  248. * @param mixed $data 要操作的数据
  249. * @return boolean
  250. */
  251. protected function _facade($data)
  252. {
  253. // 检查数据字段合法性
  254. if (!empty($this->fields)) {
  255. if (!empty($this->options['field'])) {
  256. $fields = $this->options['field'];
  257. unset($this->options['field']);
  258. if (is_string($fields)) {
  259. $fields = explode(',', $fields);
  260. }
  261. } else {
  262. $fields = $this->fields;
  263. }
  264. foreach ($data as $key => $val) {
  265. if (!in_array($key, $fields, true)) {
  266. if (!empty($this->options['strict'])) {
  267. E(L('_DATA_TYPE_INVALID_') . ':[' . $key . '=>' . $val . ']');
  268. }
  269. unset($data[$key]);
  270. } elseif (is_scalar($val)) {
  271. // 字段类型检查 和 强制转换
  272. $this->_parseType($data, $key);
  273. }
  274. }
  275. }
  276. // 安全过滤
  277. if (!empty($this->options['filter'])) {
  278. $data = array_map($this->options['filter'], $data);
  279. unset($this->options['filter']);
  280. }
  281. $this->_before_write($data);
  282. return $data;
  283. }
  284. // 写入数据前的回调方法 包括新增和更新
  285. protected function _before_write(&$data)
  286. {}
  287. /**
  288. * 新增数据
  289. * @access public
  290. * @param mixed $data 数据
  291. * @param array $options 表达式
  292. * @param boolean $replace 是否replace
  293. * @return mixed
  294. */
  295. public function add($data = '', $options = array(), $replace = false)
  296. {
  297. if (empty($data)) {
  298. // 没有传递数据,获取当前数据对象的值
  299. if (!empty($this->data)) {
  300. $data = $this->data;
  301. // 重置数据
  302. $this->data = array();
  303. } else {
  304. $this->error = L('_DATA_TYPE_INVALID_');
  305. return false;
  306. }
  307. }
  308. // 数据处理
  309. $data = $this->_facade($data);
  310. // 分析表达式
  311. $options = $this->_parseOptions($options);
  312. if (false === $this->_before_insert($data, $options)) {
  313. return false;
  314. }
  315. // 写入数据到数据库
  316. $result = $this->db->insert($data, $options, $replace);
  317. if (false !== $result && is_numeric($result)) {
  318. $pk = $this->getPk();
  319. // 增加复合主键支持
  320. if (is_array($pk)) {
  321. return $result;
  322. }
  323. $insertId = $this->getLastInsID();
  324. if ($insertId) {
  325. // 自增主键返回插入ID
  326. $data[$pk] = $insertId;
  327. if (false === $this->_after_insert($data, $options)) {
  328. return false;
  329. }
  330. return $insertId;
  331. }
  332. if (false === $this->_after_insert($data, $options)) {
  333. return false;
  334. }
  335. }
  336. return $result;
  337. }
  338. // 插入数据前的回调方法
  339. protected function _before_insert(&$data, $options)
  340. {}
  341. // 插入成功后的回调方法
  342. protected function _after_insert($data, $options)
  343. {}
  344. public function addAll($dataList, $options = array(), $replace = false)
  345. {
  346. if (empty($dataList)) {
  347. $this->error = L('_DATA_TYPE_INVALID_');
  348. return false;
  349. }
  350. // 数据处理
  351. foreach ($dataList as $key => $data) {
  352. $dataList[$key] = $this->_facade($data);
  353. }
  354. // 分析表达式
  355. $options = $this->_parseOptions($options);
  356. // 写入数据到数据库
  357. $result = $this->db->insertAll($dataList, $options, $replace);
  358. if (false !== $result) {
  359. $insertId = $this->getLastInsID();
  360. if ($insertId) {
  361. return $insertId;
  362. }
  363. }
  364. return $result;
  365. }
  366. /**
  367. * 保存数据
  368. * @access public
  369. * @param mixed $data 数据
  370. * @param array $options 表达式
  371. * @return boolean
  372. */
  373. public function save($data = '', $options = array())
  374. {
  375. if (empty($data)) {
  376. // 没有传递数据,获取当前数据对象的值
  377. if (!empty($this->data)) {
  378. $data = $this->data;
  379. // 重置数据
  380. $this->data = array();
  381. } else {
  382. $this->error = L('_DATA_TYPE_INVALID_');
  383. return false;
  384. }
  385. }
  386. // 数据处理
  387. $data = $this->_facade($data);
  388. if (empty($data)) {
  389. // 没有数据则不执行
  390. $this->error = L('_DATA_TYPE_INVALID_');
  391. return false;
  392. }
  393. // 分析表达式
  394. $options = $this->_parseOptions($options);
  395. $pk = $this->getPk();
  396. if (!isset($options['where'])) {
  397. // 如果存在主键数据 则自动作为更新条件
  398. if (is_string($pk) && isset($data[$pk])) {
  399. $where[$pk] = $data[$pk];
  400. unset($data[$pk]);
  401. } elseif (is_array($pk)) {
  402. // 增加复合主键支持
  403. foreach ($pk as $field) {
  404. if (isset($data[$field])) {
  405. $where[$field] = $data[$field];
  406. } else {
  407. // 如果缺少复合主键数据则不执行
  408. $this->error = L('_OPERATION_WRONG_');
  409. return false;
  410. }
  411. unset($data[$field]);
  412. }
  413. }
  414. if (!isset($where)) {
  415. // 如果没有任何更新条件则不执行
  416. $this->error = L('_OPERATION_WRONG_');
  417. return false;
  418. } else {
  419. $options['where'] = $where;
  420. }
  421. }
  422. if (is_array($options['where']) && isset($options['where'][$pk])) {
  423. $pkValue = $options['where'][$pk];
  424. }
  425. if (false === $this->_before_update($data, $options)) {
  426. return false;
  427. }
  428. $result = $this->db->update($data, $options);
  429. if (false !== $result && is_numeric($result)) {
  430. if (isset($pkValue)) {
  431. $data[$pk] = $pkValue;
  432. }
  433. $this->_after_update($data, $options);
  434. }
  435. return $result;
  436. }
  437. // 更新数据前的回调方法
  438. protected function _before_update(&$data, $options)
  439. {}
  440. // 更新成功后的回调方法
  441. protected function _after_update($data, $options)
  442. {}
  443. /**
  444. * 删除数据
  445. * @access public
  446. * @param mixed $options 表达式
  447. * @return mixed
  448. */
  449. public function delete($options = array())
  450. {
  451. $pk = $this->getPk();
  452. if (empty($options) && empty($this->options['where'])) {
  453. // 如果删除条件为空 则删除当前数据对象所对应的记录
  454. if (!empty($this->data) && isset($this->data[$pk])) {
  455. return $this->delete($this->data[$pk]);
  456. } else {
  457. return false;
  458. }
  459. }
  460. if (is_numeric($options) || is_string($options)) {
  461. // 根据主键删除记录
  462. if (strpos($options, ',')) {
  463. $where[$pk] = array('IN', $options);
  464. } else {
  465. $where[$pk] = $options;
  466. }
  467. $options = array();
  468. $options['where'] = $where;
  469. }
  470. // 根据复合主键删除记录
  471. if (is_array($options) && (count($options) > 0) && is_array($pk)) {
  472. $count = 0;
  473. foreach (array_keys($options) as $key) {
  474. if (is_int($key)) {
  475. $count++;
  476. }
  477. }
  478. if (count($pk) == $count) {
  479. $i = 0;
  480. foreach ($pk as $field) {
  481. $where[$field] = $options[$i];
  482. unset($options[$i++]);
  483. }
  484. $options['where'] = $where;
  485. } else {
  486. return false;
  487. }
  488. }
  489. // 分析表达式
  490. $options = $this->_parseOptions($options);
  491. if (empty($options['where'])) {
  492. // 如果条件为空 不进行删除操作 除非设置 1=1
  493. return false;
  494. }
  495. if (is_array($options['where']) && isset($options['where'][$pk])) {
  496. $pkValue = $options['where'][$pk];
  497. }
  498. if (false === $this->_before_delete($options)) {
  499. return false;
  500. }
  501. $result = $this->db->delete($options);
  502. if (false !== $result && is_numeric($result)) {
  503. $data = array();
  504. if (isset($pkValue)) {
  505. $data[$pk] = $pkValue;
  506. }
  507. $this->_after_delete($data, $options);
  508. }
  509. // 返回删除记录个数
  510. return $result;
  511. }
  512. // 删除数据前的回调方法
  513. protected function _before_delete($options)
  514. {}
  515. // 删除成功后的回调方法
  516. protected function _after_delete($data, $options)
  517. {}
  518. /**
  519. * 查询数据集
  520. * @access public
  521. * @param array $options 表达式参数
  522. * @return mixed
  523. */
  524. public function select($options = array())
  525. {
  526. $pk = $this->getPk();
  527. if (is_string($options) || is_numeric($options)) {
  528. // 根据主键查询
  529. if (strpos($options, ',')) {
  530. $where[$pk] = array('IN', $options);
  531. } else {
  532. $where[$pk] = $options;
  533. }
  534. $options = array();
  535. $options['where'] = $where;
  536. } elseif (is_array($options) && (count($options) > 0) && is_array($pk)) {
  537. // 根据复合主键查询
  538. $count = 0;
  539. foreach (array_keys($options) as $key) {
  540. if (is_int($key)) {
  541. $count++;
  542. }
  543. }
  544. if (count($pk) == $count) {
  545. $i = 0;
  546. foreach ($pk as $field) {
  547. $where[$field] = $options[$i];
  548. unset($options[$i++]);
  549. }
  550. $options['where'] = $where;
  551. } else {
  552. return false;
  553. }
  554. } elseif (false === $options) {
  555. // 用于子查询 不查询只返回SQL
  556. $options = array();
  557. // 分析表达式
  558. $options = $this->_parseOptions($options);
  559. return '( ' . $this->fetchSql(true)->select($options) . ' )';
  560. }
  561. // 分析表达式
  562. $options = $this->_parseOptions($options);
  563. // 判断查询缓存
  564. if (isset($options['cache'])) {
  565. $cache = $options['cache'];
  566. $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
  567. $data = S($key, '', $cache);
  568. if (false !== $data) {
  569. return $data;
  570. }
  571. }
  572. $resultSet = $this->db->select($options);
  573. if (false === $resultSet) {
  574. return false;
  575. }
  576. if (empty($resultSet)) {
  577. // 查询结果为空
  578. return null;
  579. }
  580. if (is_string($resultSet)) {
  581. return $resultSet;
  582. }
  583. $resultSet = array_map(array($this, '_read_data'), $resultSet);
  584. $this->_after_select($resultSet, $options);
  585. if (isset($options['index'])) {
  586. // 对数据集进行索引
  587. $index = explode(',', $options['index']);
  588. foreach ($resultSet as $result) {
  589. $_key = $result[$index[0]];
  590. if (isset($index[1]) && isset($result[$index[1]])) {
  591. $cols[$_key] = $result[$index[1]];
  592. } else {
  593. $cols[$_key] = $result;
  594. }
  595. }
  596. $resultSet = $cols;
  597. }
  598. if (isset($cache)) {
  599. S($key, $resultSet, $cache);
  600. }
  601. return $resultSet;
  602. }
  603. // 查询成功后的回调方法
  604. protected function _after_select(&$resultSet, $options)
  605. {}
  606. /**
  607. * 分析表达式
  608. * @access protected
  609. * @param array $options 表达式参数
  610. * @return array
  611. */
  612. protected function _parseOptions($options = array())
  613. {
  614. if (is_array($options)) {
  615. $options = array_merge($this->options, $options);
  616. }
  617. if (!isset($options['table'])) {
  618. // 自动获取表名
  619. $options['table'] = $this->getTableName();
  620. $fields = $this->fields;
  621. } else {
  622. // 指定数据表 则重新获取字段列表 但不支持类型检测
  623. $fields = $this->getDbFields();
  624. }
  625. // 数据表别名
  626. if (!empty($options['alias'])) {
  627. $options['table'] .= ' ' . $options['alias'];
  628. }
  629. // 记录操作的模型名称
  630. $options['model'] = $this->name;
  631. // 字段类型验证
  632. if (isset($options['where']) && is_array($options['where']) && !empty($fields) && !isset($options['join'])) {
  633. // 对数组查询条件进行字段类型检查
  634. foreach ($options['where'] as $key => $val) {
  635. $key = trim($key);
  636. if (in_array($key, $fields, true)) {
  637. if (is_scalar($val)) {
  638. $this->_parseType($options['where'], $key);
  639. }
  640. } elseif (!is_numeric($key) && '_' != substr($key, 0, 1) && false === strpos($key, '.') && false === strpos($key, '(') && false === strpos($key, '|') && false === strpos($key, '&')) {
  641. if (!empty($this->options['strict'])) {
  642. E(L('_ERROR_QUERY_EXPRESS_') . ':[' . $key . '=>' . $val . ']');
  643. }
  644. unset($options['where'][$key]);
  645. }
  646. }
  647. }
  648. // 查询过后清空sql表达式组装 避免影响下次查询
  649. $this->options = array();
  650. // 表达式过滤
  651. $this->_options_filter($options);
  652. return $options;
  653. }
  654. // 表达式过滤回调方法
  655. protected function _options_filter(&$options)
  656. {}
  657. /**
  658. * 数据类型检测
  659. * @access protected
  660. * @param mixed $data 数据
  661. * @param string $key 字段名
  662. * @return void
  663. */
  664. protected function _parseType(&$data, $key)
  665. {
  666. if (!isset($this->options['bind'][':' . $key]) && isset($this->fields['_type'][$key])) {
  667. $fieldType = strtolower($this->fields['_type'][$key]);
  668. if (false !== strpos($fieldType, 'enum')) {
  669. // 支持ENUM类型优先检测
  670. } elseif (false === strpos($fieldType, 'bigint') && false !== strpos($fieldType, 'int')) {
  671. $data[$key] = intval($data[$key]);
  672. } elseif (false !== strpos($fieldType, 'float') || false !== strpos($fieldType, 'double')) {
  673. $data[$key] = floatval($data[$key]);
  674. } elseif (false !== strpos($fieldType, 'bool')) {
  675. $data[$key] = (bool) $data[$key];
  676. }
  677. }
  678. }
  679. /**
  680. * 数据读取后的处理
  681. * @access protected
  682. * @param array $data 当前数据
  683. * @return array
  684. */
  685. protected function _read_data($data)
  686. {
  687. // 检查字段映射
  688. if (!empty($this->_map) && C('READ_DATA_MAP')) {
  689. foreach ($this->_map as $key => $val) {
  690. if (isset($data[$val])) {
  691. $data[$key] = $data[$val];
  692. unset($data[$val]);
  693. }
  694. }
  695. }
  696. return $data;
  697. }
  698. /**
  699. * 查询数据
  700. * @access public
  701. * @param mixed $options 表达式参数
  702. * @return mixed
  703. */
  704. public function find($options = array())
  705. {
  706. if (is_numeric($options) || is_string($options)) {
  707. $where[$this->getPk()] = $options;
  708. $options = array();
  709. $options['where'] = $where;
  710. }
  711. // 根据复合主键查找记录
  712. $pk = $this->getPk();
  713. if (is_array($options) && (count($options) > 0) && is_array($pk)) {
  714. // 根据复合主键查询
  715. $count = 0;
  716. foreach (array_keys($options) as $key) {
  717. if (is_int($key)) {
  718. $count++;
  719. }
  720. }
  721. if (count($pk) == $count) {
  722. $i = 0;
  723. foreach ($pk as $field) {
  724. $where[$field] = $options[$i];
  725. unset($options[$i++]);
  726. }
  727. $options['where'] = $where;
  728. } else {
  729. return false;
  730. }
  731. }
  732. // 总是查找一条记录
  733. $options['limit'] = 1;
  734. // 分析表达式
  735. $options = $this->_parseOptions($options);
  736. // 判断查询缓存
  737. if (isset($options['cache'])) {
  738. $cache = $options['cache'];
  739. $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
  740. $data = S($key, '', $cache);
  741. if (false !== $data) {
  742. $this->data = $data;
  743. return $data;
  744. }
  745. }
  746. $resultSet = $this->db->select($options);
  747. if (false === $resultSet) {
  748. return false;
  749. }
  750. if (empty($resultSet)) {
  751. // 查询结果为空
  752. return null;
  753. }
  754. if (is_string($resultSet)) {
  755. return $resultSet;
  756. }
  757. // 读取数据后的处理
  758. $data = $this->_read_data($resultSet[0]);
  759. $this->_after_find($data, $options);
  760. $this->data = $data;
  761. if (isset($cache)) {
  762. S($key, $data, $cache);
  763. }
  764. return $this->data;
  765. }
  766. // 查询成功的回调方法
  767. protected function _after_find(&$result, $options)
  768. {}
  769. /**
  770. * 设置记录的某个字段值
  771. * 支持使用数据库字段和方法
  772. * @access public
  773. * @param string|array $field 字段名
  774. * @param string $value 字段值
  775. * @return boolean
  776. */
  777. public function setField($field, $value = '')
  778. {
  779. if (is_array($field)) {
  780. $data = $field;
  781. } else {
  782. $data[$field] = $value;
  783. }
  784. return $this->save($data);
  785. }
  786. /**
  787. * 字段值增长
  788. * @access public
  789. * @param string $field 字段名
  790. * @param integer $step 增长值
  791. * @param integer $lazyTime 延时时间(s)
  792. * @return boolean
  793. */
  794. public function setInc($field, $step = 1)
  795. {
  796. return $this->setField($field, array('exp', $field . '+' . $step));
  797. }
  798. /**
  799. * 字段值减少
  800. * @access public
  801. * @param string $field 字段名
  802. * @param integer $step 减少值
  803. * @param integer $lazyTime 延时时间(s)
  804. * @return boolean
  805. */
  806. public function setDec($field, $step = 1)
  807. {
  808. return $this->setField($field, array('exp', $field . '-' . $step));
  809. }
  810. /**
  811. * 获取一条记录的某个字段值
  812. * @access public
  813. * @param string $field 字段名
  814. * @param string $spea 字段数据间隔符号 NULL返回数组
  815. * @return mixed
  816. */
  817. public function getField($field, $sepa = null)
  818. {
  819. $options['field'] = $field;
  820. $options = $this->_parseOptions($options);
  821. // 判断查询缓存
  822. if (isset($options['cache'])) {
  823. $cache = $options['cache'];
  824. $key = is_string($cache['key']) ? $cache['key'] : md5($sepa . serialize($options));
  825. $data = S($key, '', $cache);
  826. if (false !== $data) {
  827. return $data;
  828. }
  829. }
  830. $field = trim($field);
  831. if (strpos($field, ',') && false !== $sepa) {
  832. // 多字段
  833. if (!isset($options['limit'])) {
  834. $options['limit'] = is_numeric($sepa) ? $sepa : '';
  835. }
  836. $resultSet = $this->db->select($options);
  837. if (!empty($resultSet)) {
  838. $_field = explode(',', $field);
  839. $field = array_keys($resultSet[0]);
  840. $key1 = array_shift($field);
  841. $key2 = array_shift($field);
  842. $cols = array();
  843. $count = count($_field);
  844. foreach ($resultSet as $result) {
  845. $name = $result[$key1];
  846. if (2 == $count) {
  847. $cols[$name] = $result[$key2];
  848. } else {
  849. $cols[$name] = is_string($sepa) ? implode($sepa, array_slice($result, 1)) : $result;
  850. }
  851. }
  852. if (isset($cache)) {
  853. S($key, $cols, $cache);
  854. }
  855. return $cols;
  856. }
  857. } else {
  858. // 查找一条记录
  859. // 返回数据个数
  860. if (true !== $sepa) { // 当sepa指定为true的时候 返回所有数据
  861. $options['limit'] = is_numeric($sepa) ? $sepa : 1;
  862. }
  863. $result = $this->db->select($options);
  864. if (!empty($result)) {
  865. if (true !== $sepa && 1 == $options['limit']) {
  866. $data = reset($result[0]);
  867. if (isset($cache)) {
  868. S($key, $data, $cache);
  869. }
  870. return $data;
  871. }
  872. foreach ($result as $val) {
  873. $array[] = $val[$field];
  874. }
  875. if (isset($cache)) {
  876. S($key, $array, $cache);
  877. }
  878. return $array;
  879. }
  880. }
  881. return null;
  882. }
  883. /**
  884. * 创建数据对象 但不保存到数据库
  885. * @access public
  886. * @param mixed $data 创建数据
  887. * @return mixed
  888. */
  889. public function create($data = '')
  890. {
  891. // 如果没有传值默认取POST数据
  892. if (empty($data)) {
  893. $data = I('post.');
  894. } elseif (is_object($data)) {
  895. $data = get_object_vars($data);
  896. }
  897. // 验证数据
  898. if (empty($data) || !is_array($data)) {
  899. $this->error = L('_DATA_TYPE_INVALID_');
  900. return false;
  901. }
  902. // 检测提交字段的合法性
  903. if (isset($this->options['field'])) {
  904. // $this->field('field1,field2...')->create()
  905. $fields = $this->options['field'];
  906. unset($this->options['field']);
  907. }
  908. if (isset($fields)) {
  909. if (is_string($fields)) {
  910. $fields = explode(',', $fields);
  911. }
  912. }
  913. // 验证完成生成数据对象
  914. if ($this->autoCheckFields) {
  915. // 开启字段检测 则过滤非法字段数据
  916. $fields = $this->getDbFields();
  917. foreach ($data as $key => $val) {
  918. if (!in_array($key, $fields)) {
  919. unset($data[$key]);
  920. } elseif (MAGIC_QUOTES_GPC && is_string($val)) {
  921. $data[$key] = stripslashes($val);
  922. }
  923. }
  924. }
  925. // 赋值当前数据对象
  926. $this->data = $data;
  927. // 返回创建的数据以供其他调用
  928. return $data;
  929. }
  930. /**
  931. * 使用正则验证数据
  932. * @access public
  933. * @param string $value 要验证的数据
  934. * @param string $rule 验证规则
  935. * @return boolean
  936. */
  937. public function regex($value, $rule)
  938. {
  939. $validate = array(
  940. 'require' => '/\S+/',
  941. 'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
  942. 'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(:\d+)?(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
  943. 'currency' => '/^\d+(\.\d+)?$/',
  944. 'number' => '/^\d+$/',
  945. 'zip' => '/^\d{6}$/',
  946. 'integer' => '/^[-\+]?\d+$/',
  947. 'double' => '/^[-\+]?\d+(\.\d+)?$/',
  948. 'english' => '/^[A-Za-z]+$/',
  949. );
  950. // 检查是否有内置的正则表达式
  951. if (isset($validate[strtolower($rule)])) {
  952. $rule = $validate[strtolower($rule)];
  953. }
  954. return preg_match($rule, $value) === 1;
  955. }
  956. /**
  957. * 验证数据 支持 in between equal length regex expire ip_allow ip_deny
  958. * @access public
  959. * @param string $value 验证数据
  960. * @param mixed $rule 验证表达式
  961. * @param string $type 验证方式 默认为正则验证
  962. * @return boolean
  963. */
  964. public function check($value, $rule, $type = 'regex')
  965. {
  966. $type = strtolower(trim($type));
  967. switch ($type) {
  968. case 'in': // 验证是否在某个指定范围之内 逗号分隔字符串或者数组
  969. case 'notin':
  970. $range = is_array($rule) ? $rule : explode(',', $rule);
  971. return 'in' == $type ? in_array($value, $range) : !in_array($value, $range);
  972. case 'between': // 验证是否在某个范围
  973. case 'notbetween': // 验证是否不在某个范围
  974. if (is_array($rule)) {
  975. $min = $rule[0];
  976. $max = $rule[1];
  977. } else {
  978. list($min, $max) = explode(',', $rule);
  979. }
  980. return 'between' == $type ? $value >= $min && $value <= $max : $value < $min || $value > $max;
  981. case 'equal': // 验证是否等于某个值
  982. case 'notequal': // 验证是否等于某个值
  983. return 'equal' == $type ? $value == $rule : $value != $rule;
  984. case 'length': // 验证长度
  985. $length = mb_strlen($value, 'utf-8'); // 当前数据长度
  986. if (strpos($rule, ',')) {
  987. // 长度区间
  988. list($min, $max) = explode(',', $rule);
  989. return $length >= $min && $length <= $max;
  990. } else {
  991. // 指定长度
  992. return $length == $rule;
  993. }
  994. case 'expire':
  995. list($start, $end) = explode(',', $rule);
  996. if (!is_numeric($start)) {
  997. $start = strtotime($start);
  998. }
  999. if (!is_numeric($end)) {
  1000. $end = strtotime($end);
  1001. }
  1002. return NOW_TIME >= $start && NOW_TIME <= $end;
  1003. case 'ip_allow': // IP 操作许可验证
  1004. return in_array(get_client_ip(), explode(',', $rule));
  1005. case 'ip_deny': // IP 操作禁止验证
  1006. return !in_array(get_client_ip(), explode(',', $rule));
  1007. case 'regex':
  1008. default: // 默认使用正则验证 可以使用验证类中定义的验证名称
  1009. // 检查附加规则
  1010. return $this->regex($value, $rule);
  1011. }
  1012. }
  1013. /**
  1014. * SQL查询
  1015. * @access public
  1016. * @param string $sql SQL指令
  1017. * @return mixed
  1018. */
  1019. public function query($sql)
  1020. {
  1021. return $this->db->query($sql);
  1022. }
  1023. /**
  1024. * 执行SQL语句
  1025. * @access public
  1026. * @param string $sql SQL指令
  1027. * @return false | integer
  1028. */
  1029. public function execute($sql)
  1030. {
  1031. return $this->db->execute($sql);
  1032. }
  1033. /**
  1034. * 切换当前的数据库连接
  1035. * @access public
  1036. * @param integer $linkNum 连接序号
  1037. * @param mixed $config 数据库连接信息
  1038. * @param boolean $force 强制重新连接
  1039. * @return Model
  1040. */
  1041. public function db($linkNum = '', $config = '', $force = false)
  1042. {
  1043. if ('' === $linkNum && $this->db) {
  1044. return $this->db;
  1045. }
  1046. if (!isset($this->_db[$linkNum]) || $force) {
  1047. // 创建一个新的实例
  1048. if (!empty($config) && is_string($config) && false === strpos($config, '/')) {
  1049. // 支持读取配置参数
  1050. $config = C($config);
  1051. }
  1052. $this->_db[$linkNum] = Db::getInstance($config);
  1053. } elseif (null === $config) {
  1054. $this->_db[$linkNum]->close(); // 关闭数据库连接
  1055. unset($this->_db[$linkNum]);
  1056. return;
  1057. }
  1058. // 切换数据库连接
  1059. $this->db = $this->_db[$linkNum];
  1060. $this->_after_db();
  1061. // 字段检测
  1062. if (!empty($this->name) && $this->autoCheckFields) {
  1063. $this->_checkTableInfo();
  1064. }
  1065. return $this;
  1066. }
  1067. // 数据库切换后回调方法
  1068. protected function _after_db()
  1069. {}
  1070. /**
  1071. * 得到当前的数据对象名称
  1072. * @access public
  1073. * @return string
  1074. */
  1075. public function getModelName()
  1076. {
  1077. if (empty($this->name)) {
  1078. $name = substr(get_class($this), 0, -strlen(C('DEFAULT_M_LAYER')));
  1079. if ($pos = strrpos($name, '\\')) {
  1080. //有命名空间
  1081. $this->name = substr($name, $pos + 1);
  1082. } else {
  1083. $this->name = $name;
  1084. }
  1085. }
  1086. return $this->name;
  1087. }
  1088. /**
  1089. * 得到完整的数据表名
  1090. * @access public
  1091. * @return string
  1092. */
  1093. public function getTableName()
  1094. {
  1095. if (empty($this->trueTableName)) {
  1096. $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : '';
  1097. if (!empty($this->tableName)) {
  1098. $tableName .= $this->tableName;
  1099. } else {
  1100. $tableName .= parse_name($this->name);
  1101. }
  1102. $this->trueTableName = strtolower($tableName);
  1103. }
  1104. return (!empty($this->dbName) ? $this->dbName . '.' : '') . $this->trueTableName;
  1105. }
  1106. /**
  1107. * 启动事务
  1108. * @access public
  1109. * @return void
  1110. */
  1111. public function startTrans()
  1112. {
  1113. $this->commit();
  1114. $this->db->startTrans();
  1115. return;
  1116. }
  1117. /**
  1118. * 提交事务
  1119. * @access public
  1120. * @return boolean
  1121. */
  1122. public function commit()
  1123. {
  1124. return $this->db->commit();
  1125. }
  1126. /**
  1127. * 事务回滚
  1128. * @access public
  1129. * @return boolean
  1130. */
  1131. public function rollback()
  1132. {
  1133. return $this->db->rollback();
  1134. }
  1135. /**
  1136. * 返回模型的错误信息
  1137. * @access public
  1138. * @return string
  1139. */
  1140. public function getError()
  1141. {
  1142. return $this->error;
  1143. }
  1144. /**
  1145. * 返回数据库的错误信息
  1146. * @access public
  1147. * @return string
  1148. */
  1149. public function getDbError()
  1150. {
  1151. return $this->db->getError();
  1152. }
  1153. /**
  1154. * 返回最后插入的ID
  1155. * @access public
  1156. * @return string
  1157. */
  1158. public function getLastInsID()
  1159. {
  1160. return $this->db->getLastInsID();
  1161. }
  1162. /**
  1163. * 返回最后执行的sql语句
  1164. * @access public
  1165. * @return string
  1166. */
  1167. public function getLastSql()
  1168. {
  1169. return $this->db->getLastSql($this->name);
  1170. }
  1171. // 鉴于getLastSql比较常用 增加_sql 别名
  1172. public function _sql()
  1173. {
  1174. return $this->getLastSql();
  1175. }
  1176. /**
  1177. * 获取主键名称
  1178. * @access public
  1179. * @return string
  1180. */
  1181. public function getPk()
  1182. {
  1183. return $this->pk;
  1184. }
  1185. /**
  1186. * 获取数据表字段信息
  1187. * @access public
  1188. * @return array
  1189. */
  1190. public function getDbFields()
  1191. {
  1192. if (isset($this->options['table'])) {
  1193. // 动态指定表名
  1194. if (is_array($this->options['table'])) {
  1195. $table = key($this->options['table']);
  1196. } else {
  1197. $table = $this->options['table'];
  1198. }
  1199. $fields = $this->db->getFields($table);
  1200. return $fields ? array_keys($fields) : false;
  1201. }
  1202. if ($this->fields) {
  1203. $fields = $this->fields;
  1204. unset($fields['_type'], $fields['_pk']);
  1205. return $fields;
  1206. }
  1207. return false;
  1208. }
  1209. /**
  1210. * 设置数据对象值
  1211. * @access public
  1212. * @param mixed $data 数据
  1213. * @return Model
  1214. */
  1215. public function data($data = '')
  1216. {
  1217. if ('' === $data && !empty($this->data)) {
  1218. return $this->data;
  1219. }
  1220. if (is_object($data)) {
  1221. $data = get_object_vars($data);
  1222. } elseif (is_string($data)) {
  1223. parse_str($data, $data);
  1224. } elseif (!is_array($data)) {
  1225. E(L('_DATA_TYPE_INVALID_'));
  1226. }
  1227. $this->data = $data;
  1228. return $this;
  1229. }
  1230. /**
  1231. * 指定当前的数据表
  1232. * @access public
  1233. * @param mixed $table
  1234. * @return Model
  1235. */
  1236. public function table($table)
  1237. {
  1238. $prefix = $this->tablePrefix;
  1239. if (is_array($table)) {
  1240. $this->options['table'] = $table;
  1241. } elseif (!empty($table)) {
  1242. //将__TABLE_NAME__替换成带前缀的表名
  1243. $table = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $table);
  1244. $this->options['table'] = $table;
  1245. }
  1246. return $this;
  1247. }
  1248. /**
  1249. * USING支持 用于多表删除
  1250. * @access public
  1251. * @param mixed $using
  1252. * @return Model
  1253. */
  1254. public function using($using)
  1255. {
  1256. $prefix = $this->tablePrefix;
  1257. if (is_array($using)) {
  1258. $this->options['using'] = $using;
  1259. } elseif (!empty($using)) {
  1260. //将__TABLE_NAME__替换成带前缀的表名
  1261. $using = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $using);
  1262. $this->options['using'] = $using;
  1263. }
  1264. return $this;
  1265. }
  1266. /**
  1267. * 查询SQL组装 join
  1268. * @access public
  1269. * @param mixed $join
  1270. * @param string $type JOIN类型
  1271. * @return Model
  1272. */
  1273. public function join($join, $type = 'INNER')
  1274. {
  1275. $prefix = $this->tablePrefix;
  1276. if (is_array($join)) {
  1277. foreach ($join as $key => &$_join) {
  1278. $_join = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $_join);
  1279. $_join = false !== stripos($_join, 'JOIN') ? $_join : $type . ' JOIN ' . $_join;
  1280. }
  1281. $this->options['join'] = $join;
  1282. } elseif (!empty($join)) {
  1283. //将__TABLE_NAME__字符串替换成带前缀的表名
  1284. $join = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $join);
  1285. $this->options['join'][] = false !== stripos($join, 'JOIN') ? $join : $type . ' JOIN ' . $join;
  1286. }
  1287. return $this;
  1288. }
  1289. /**
  1290. * 查询SQL组装 union
  1291. * @access public
  1292. * @param mixed $union
  1293. * @param boolean $all
  1294. * @return Model
  1295. */
  1296. public function union($union, $all = false)
  1297. {
  1298. if (empty($union)) {
  1299. return $this;
  1300. }
  1301. if ($all) {
  1302. $this->options['union']['_all'] = true;
  1303. }
  1304. if (is_object($union)) {
  1305. $union = get_object_vars($union);
  1306. }
  1307. // 转换union表达式
  1308. if (is_string($union)) {
  1309. $prefix = $this->tablePrefix;
  1310. //将__TABLE_NAME__字符串替换成带前缀的表名
  1311. $options = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $union);
  1312. } elseif (is_array($union)) {
  1313. if (isset($union[0])) {
  1314. $this->options['union'] = array_merge($this->options['union'], $union);
  1315. return $this;
  1316. } else {
  1317. $options = $union;
  1318. }
  1319. } else {
  1320. E(L('_DATA_TYPE_INVALID_'));
  1321. }
  1322. $this->options['union'][] = $options;
  1323. return $this;
  1324. }
  1325. /**
  1326. * 查询缓存
  1327. * @access public
  1328. * @param mixed $key
  1329. * @param integer $expire
  1330. * @param string $type
  1331. * @return Model
  1332. */
  1333. public function cache($key = true, $expire = null, $type = '')
  1334. {
  1335. // 增加快捷调用方式 cache(10) 等同于 cache(true, 10)
  1336. if (is_numeric($key) && is_null($expire)) {
  1337. $expire = $key;
  1338. $key = true;
  1339. }
  1340. if (false !== $key) {
  1341. $this->options['cache'] = array('key' => $key, 'expire' => $expire, 'type' => $type);
  1342. }
  1343. return $this;
  1344. }
  1345. /**
  1346. * 指定查询字段 支持字段排除
  1347. * @access public
  1348. * @param mixed $field
  1349. * @param boolean $except 是否排除
  1350. * @return Model
  1351. */
  1352. public function field($field, $except = false)
  1353. {
  1354. if (true === $field) {
  1355. // 获取全部字段
  1356. $fields = $this->getDbFields();
  1357. $field = $fields ?: '*';
  1358. } elseif ($except) {
  1359. // 字段排除
  1360. if (is_string($field)) {
  1361. $field = explode(',', $field);
  1362. }
  1363. $fields = $this->getDbFields();
  1364. $field = $fields ? array_diff($fields, $field) : $field;
  1365. }
  1366. $this->options['field'] = $field;
  1367. return $this;
  1368. }
  1369. /**
  1370. * 调用命名范围
  1371. * @access public
  1372. * @param mixed $scope 命名范围名称 支持多个 和直接定义
  1373. * @param array $args 参数
  1374. * @return Model
  1375. */
  1376. public function scope($scope = '', $args = null)
  1377. {
  1378. if ('' === $scope) {
  1379. if (isset($this->_scope['default'])) {
  1380. // 默认的命名范围
  1381. $options = $this->_scope['default'];
  1382. } else {
  1383. return $this;
  1384. }
  1385. } elseif (is_string($scope)) {
  1386. // 支持多个命名范围调用 用逗号分割
  1387. $scopes = explode(',', $scope);
  1388. $options = array();
  1389. foreach ($scopes as $name) {
  1390. if (!isset($this->_scope[$name])) {
  1391. continue;
  1392. }
  1393. $options = array_merge($options, $this->_scope[$name]);
  1394. }
  1395. if (!empty($args) && is_array($args)) {
  1396. $options = array_merge($options, $args);
  1397. }
  1398. } elseif (is_array($scope)) {
  1399. // 直接传入命名范围定义
  1400. $options = $scope;
  1401. }
  1402. if (is_array($options) && !empty($options)) {
  1403. $this->options = array_merge($this->options, array_change_key_case($options));
  1404. }
  1405. return $this;
  1406. }
  1407. /**
  1408. * 指定查询条件 支持安全过滤
  1409. * @access public
  1410. * @param mixed $where 条件表达式
  1411. * @param mixed $parse 预处理参数
  1412. * @return Model
  1413. */
  1414. public function where($where, $parse = null)
  1415. {
  1416. if (!is_null($parse) && is_string($where)) {
  1417. if (!is_array($parse)) {
  1418. $parse = func_get_args();
  1419. array_shift($parse);
  1420. }
  1421. $parse = array_map(array($this->db, 'escapeString'), $parse);
  1422. $where = vsprintf($where, $parse);
  1423. } elseif (is_object($where)) {
  1424. $where = get_object_vars($where);
  1425. }
  1426. if (is_string($where) && '' != $where) {
  1427. $map = array();
  1428. $map['_string'] = $where;
  1429. $where = $map;
  1430. }
  1431. if (isset($this->options['where'])) {
  1432. $this->options['where'] = array_merge($this->options['where'], $where);
  1433. } else {
  1434. $this->options['where'] = $where;
  1435. }
  1436. return $this;
  1437. }
  1438. /**
  1439. * 指定查询数量
  1440. * @access public
  1441. * @param mixed $offset 起始位置
  1442. * @param mixed $length 查询数量
  1443. * @return Model
  1444. */
  1445. public function limit($offset, $length = null)
  1446. {
  1447. if (is_null($length) && strpos($offset, ',')) {
  1448. list($offset, $length) = explode(',', $offset);
  1449. }
  1450. $this->options['limit'] = intval($offset) . ($length ? ',' . intval($length) : '');
  1451. return $this;
  1452. }
  1453. /**
  1454. * 指定分页
  1455. * @access public
  1456. * @param mixed $page 页数
  1457. * @param mixed $listRows 每页数量
  1458. * @return Model
  1459. */
  1460. public function page($page, $listRows = null)
  1461. {
  1462. if (is_null($listRows) && strpos($page, ',')) {
  1463. list($page, $listRows) = explode(',', $page);
  1464. }
  1465. $this->options['page'] = array(intval($page), intval($listRows));
  1466. return $this;
  1467. }
  1468. /**
  1469. * 查询注释
  1470. * @access public
  1471. * @param string $comment 注释
  1472. * @return Model
  1473. */
  1474. public function comment($comment)
  1475. {
  1476. $this->options['comment'] = $comment;
  1477. return $this;
  1478. }
  1479. /**
  1480. * 获取执行的SQL语句
  1481. * @access public
  1482. * @param boolean $fetch 是否返回sql
  1483. * @return Model
  1484. */
  1485. public function fetchSql($fetch)
  1486. {
  1487. $this->options['fetch_sql'] = $fetch;
  1488. return $this;
  1489. }
  1490. /**
  1491. * 参数绑定
  1492. * @access public
  1493. * @param string $key 参数名
  1494. * @param mixed $value 绑定的变量及绑定参数
  1495. * @return Model
  1496. */
  1497. public function bind($key, $value = false)
  1498. {
  1499. if (is_array($key)) {
  1500. $this->options['bind'] = $key;
  1501. } else {
  1502. $num = func_num_args();
  1503. if ($num > 2) {
  1504. $params = func_get_args();
  1505. array_shift($params);
  1506. $this->options['bind'][$key] = $params;
  1507. } else {
  1508. $this->options['bind'][$key] = $value;
  1509. }
  1510. }
  1511. return $this;
  1512. }
  1513. /**
  1514. * 设置模型的属性值
  1515. * @access public
  1516. * @param string $name 名称
  1517. * @param mixed $value 值
  1518. * @return Model
  1519. */
  1520. public function setProperty($name, $value)
  1521. {
  1522. if (property_exists($this, $name)) {
  1523. $this->$name = $value;
  1524. }
  1525. return $this;
  1526. }
  1527. }