MongoModel.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2010 http://topthink.com 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\Model;
  12. use Think\Model;
  13. /**
  14. * MongoModel模型类
  15. * 实现了ODM和ActiveRecords模式
  16. */
  17. class MongoModel extends Model
  18. {
  19. // 主键类型
  20. const TYPE_OBJECT = 1;
  21. const TYPE_INT = 2;
  22. const TYPE_STRING = 3;
  23. // 主键名称
  24. protected $pk = '_id';
  25. // _id 类型 1 Object 采用MongoId对象 2 Int 整形 支持自动增长 3 String 字符串Hash
  26. protected $_idType = self::TYPE_OBJECT;
  27. // 主键是否自增
  28. protected $_autoinc = true;
  29. // Mongo默认关闭字段检测 可以动态追加字段
  30. protected $autoCheckFields = false;
  31. // 链操作方法列表
  32. protected $methods = array('table', 'order', 'auto', 'filter', 'validate');
  33. /**
  34. * 利用__call方法实现一些特殊的Model方法
  35. * @access public
  36. * @param string $method 方法名称
  37. * @param array $args 调用参数
  38. * @return mixed
  39. */
  40. public function __call($method, $args)
  41. {
  42. if (in_array(strtolower($method), $this->methods, true)) {
  43. // 连贯操作的实现
  44. $this->options[strtolower($method)] = $args[0];
  45. return $this;
  46. } elseif (strtolower(substr($method, 0, 5)) == 'getby') {
  47. // 根据某个字段获取记录
  48. $field = parse_name(substr($method, 5));
  49. $where[$field] = $args[0];
  50. return $this->where($where)->find();
  51. } elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') {
  52. // 根据某个字段获取记录的某个值
  53. $name = parse_name(substr($method, 10));
  54. $where[$name] = $args[0];
  55. return $this->where($where)->getField($args[1]);
  56. } else {
  57. E(__CLASS__ . ':' . $method . L('_METHOD_NOT_EXIST_'));
  58. return;
  59. }
  60. }
  61. /**
  62. * 获取字段信息并缓存 主键和自增信息直接配置
  63. * @access public
  64. * @return void
  65. */
  66. public function flush()
  67. {
  68. // 缓存不存在则查询数据表信息
  69. $fields = $this->db->getFields();
  70. if (!$fields) {
  71. // 暂时没有数据无法获取字段信息 下次查询
  72. return false;
  73. }
  74. $this->fields = array_keys($fields);
  75. foreach ($fields as $key => $val) {
  76. // 记录字段类型
  77. $type[$key] = $val['type'];
  78. }
  79. // 记录字段类型信息
  80. if (C('DB_FIELDTYPE_CHECK')) {
  81. $this->fields['_type'] = $type;
  82. }
  83. // 2008-3-7 增加缓存开关控制
  84. if (C('DB_FIELDS_CACHE')) {
  85. // 永久缓存数据表信息
  86. $db = $this->dbName ? $this->dbName : C('DB_NAME');
  87. F('_fields/' . $db . '.' . $this->name, $this->fields);
  88. }
  89. }
  90. // 写入数据前的回调方法 包括新增和更新
  91. protected function _before_write(&$data)
  92. {
  93. $pk = $this->getPk();
  94. // 根据主键类型处理主键数据
  95. if (isset($data[$pk]) && self::TYPE_OBJECT == $this->_idType) {
  96. $data[$pk] = new \MongoId($data[$pk]);
  97. }
  98. }
  99. /**
  100. * count统计 配合where连贯操作
  101. * @access public
  102. * @return integer
  103. */
  104. public function count()
  105. {
  106. // 分析表达式
  107. $options = $this->_parseOptions();
  108. return $this->db->count($options);
  109. }
  110. /**
  111. * 获取唯一值
  112. * @access public
  113. * @return array | false
  114. */
  115. public function distinct($field, $where = array())
  116. {
  117. // 分析表达式
  118. $this->options = $this->_parseOptions();
  119. $this->options['where'] = array_merge((array) $this->options['where'], $where);
  120. $command = array(
  121. "distinct" => $this->options['table'],
  122. "key" => $field,
  123. "query" => $this->options['where'],
  124. );
  125. $result = $this->command($command);
  126. return isset($result['values']) ? $result['values'] : false;
  127. }
  128. /**
  129. * 获取下一ID 用于自动增长型
  130. * @access public
  131. * @param string $pk 字段名 默认为主键
  132. * @return mixed
  133. */
  134. public function getMongoNextId($pk = '')
  135. {
  136. if (empty($pk)) {
  137. $pk = $this->getPk();
  138. }
  139. $options = $this->_parseOptions();
  140. return $this->db->getMongoNextId($pk, $options);
  141. }
  142. /**
  143. * 新增数据
  144. * @access public
  145. * @param mixed $data 数据
  146. * @param array $options 表达式
  147. * @param boolean $replace 是否replace
  148. * @return mixed
  149. */
  150. public function add($data = '', $options = array(), $replace = false)
  151. {
  152. if (empty($data)) {
  153. // 没有传递数据,获取当前数据对象的值
  154. if (!empty($this->data)) {
  155. $data = $this->data;
  156. // 重置数据
  157. $this->data = array();
  158. } else {
  159. $this->error = L('_DATA_TYPE_INVALID_');
  160. return false;
  161. }
  162. }
  163. // 分析表达式
  164. $options = $this->_parseOptions($options);
  165. // 数据处理
  166. $data = $this->_facade($data);
  167. if (false === $this->_before_insert($data, $options)) {
  168. return false;
  169. }
  170. // 写入数据到数据库
  171. $result = $this->db->insert($data, $options, $replace);
  172. if (false !== $result) {
  173. $this->_after_insert($data, $options);
  174. if (isset($data[$this->getPk()])) {
  175. return $data[$this->getPk()];
  176. }
  177. }
  178. return $result;
  179. }
  180. // 插入数据前的回调方法
  181. protected function _before_insert(&$data, $options)
  182. {
  183. // 写入数据到数据库
  184. if ($this->_autoinc && self::TYPE_INT == $this->_idType) {
  185. // 主键自动增长
  186. $pk = $this->getPk();
  187. if (!isset($data[$pk])) {
  188. $data[$pk] = $this->db->getMongoNextId($pk, $options);
  189. }
  190. }
  191. }
  192. public function clear()
  193. {
  194. return $this->db->clear();
  195. }
  196. // 查询成功后的回调方法
  197. protected function _after_select(&$resultSet, $options)
  198. {
  199. array_walk($resultSet, array($this, 'checkMongoId'));
  200. }
  201. /**
  202. * 获取MongoId
  203. * @access protected
  204. * @param array $result 返回数据
  205. * @return array
  206. */
  207. protected function checkMongoId(&$result)
  208. {
  209. if (is_object($result['_id'])) {
  210. $result['_id'] = $result['_id']->__toString();
  211. }
  212. return $result;
  213. }
  214. // 表达式过滤回调方法
  215. protected function _options_filter(&$options)
  216. {
  217. $id = $this->getPk();
  218. if (isset($options['where'][$id]) && is_scalar($options['where'][$id]) && self::TYPE_OBJECT == $this->_idType) {
  219. $options['where'][$id] = new \MongoId($options['where'][$id]);
  220. }
  221. }
  222. /**
  223. * 查询多行数据
  224. * @access public
  225. * @param mixed $options 表达式参数
  226. * @return mixed
  227. */
  228. public function select($options = array())
  229. {
  230. if (is_numeric($options) || is_string($options)) {
  231. $id = $this->getPk();
  232. $where[$id] = $options;
  233. $options = array();
  234. $options['where'] = $where;
  235. }
  236. // 分析表达式
  237. $options = $this->_parseOptions($options);
  238. $result = $this->db->select($options);
  239. if (false === $result) {
  240. return false;
  241. }
  242. if (empty($result)) { // 查询结果为空
  243. return null;
  244. } else {
  245. $this->checkMongoId($result);
  246. }
  247. //$result是以主键为key的,所以需要处理一下
  248. $data = array();
  249. foreach ($result as $v) {
  250. $data[] = $v;
  251. }
  252. $this->data = $data;
  253. $this->_after_select($this->data, $options);
  254. return $this->data;
  255. }
  256. /**
  257. * 查询数据
  258. * @access public
  259. * @param mixed $options 表达式参数
  260. * @return mixed
  261. */
  262. public function find($options = array())
  263. {
  264. if (is_numeric($options) || is_string($options)) {
  265. $id = $this->getPk();
  266. $where[$id] = $options;
  267. $options = array();
  268. $options['where'] = $where;
  269. }
  270. // 分析表达式
  271. $options = $this->_parseOptions($options);
  272. $result = $this->db->find($options);
  273. if (false === $result) {
  274. return false;
  275. }
  276. if (empty($result)) {
  277. // 查询结果为空
  278. return null;
  279. } else {
  280. $this->checkMongoId($result);
  281. }
  282. $this->data = $result;
  283. $this->_after_find($this->data, $options);
  284. return $this->data;
  285. }
  286. /**
  287. * 字段值增长
  288. * @access public
  289. * @param string $field 字段名
  290. * @param integer $step 增长值
  291. * @return boolean
  292. */
  293. public function setInc($field, $step = 1)
  294. {
  295. return $this->setField($field, array('inc', $step));
  296. }
  297. /**
  298. * 字段值减少
  299. * @access public
  300. * @param string $field 字段名
  301. * @param integer $step 减少值
  302. * @return boolean
  303. */
  304. public function setDec($field, $step = 1)
  305. {
  306. return $this->setField($field, array('inc', '-' . $step));
  307. }
  308. /**
  309. * 获取一条记录的某个字段值
  310. * @access public
  311. * @param string $field 字段名
  312. * @param string $spea 字段数据间隔符号
  313. * @return mixed
  314. */
  315. public function getField($field, $sepa = null)
  316. {
  317. $options['field'] = $field;
  318. $options = $this->_parseOptions($options);
  319. if (strpos($field, ',')) {
  320. // 多字段
  321. if (is_numeric($sepa)) { // 限定数量
  322. $options['limit'] = $sepa;
  323. $sepa = null; // 重置为null 返回数组
  324. }
  325. $resultSet = $this->db->select($options);
  326. if (!empty($resultSet)) {
  327. $_field = explode(',', $field);
  328. $field = array_keys($resultSet[0]);
  329. $key = array_shift($field);
  330. $key2 = array_shift($field);
  331. $cols = array();
  332. $count = count($_field);
  333. foreach ($resultSet as $result) {
  334. $name = $result[$key];
  335. if (2 == $count) {
  336. $cols[$name] = $result[$key2];
  337. } else {
  338. $cols[$name] = is_null($sepa) ? $result : implode($sepa, $result);
  339. }
  340. }
  341. return $cols;
  342. }
  343. } else {
  344. // 返回数据个数
  345. if (true !== $sepa) {
  346. // 当sepa指定为true的时候 返回所有数据
  347. $options['limit'] = is_numeric($sepa) ? $sepa : 1;
  348. } // 查找符合的记录
  349. $result = $this->db->select($options);
  350. if (!empty($result)) {
  351. if (1 == $options['limit']) {
  352. $result = reset($result);
  353. return $result[$field];
  354. }
  355. foreach ($result as $val) {
  356. $array[] = $val[$field];
  357. }
  358. return $array;
  359. }
  360. }
  361. return null;
  362. }
  363. /**
  364. * 执行Mongo指令
  365. * @access public
  366. * @param array $command 指令
  367. * @return mixed
  368. */
  369. public function command($command, $options = array())
  370. {
  371. $options = $this->_parseOptions($options);
  372. return $this->db->command($command, $options);
  373. }
  374. /**
  375. * 执行MongoCode
  376. * @access public
  377. * @param string $code MongoCode
  378. * @param array $args 参数
  379. * @return mixed
  380. */
  381. public function mongoCode($code, $args = array())
  382. {
  383. return $this->db->execute($code, $args);
  384. }
  385. // 数据库切换后回调方法
  386. protected function _after_db()
  387. {
  388. // 切换Collection
  389. $this->db->switchCollection($this->getTableName(), $this->dbName ? $this->dbName : C('db_name'));
  390. }
  391. /**
  392. * 得到完整的数据表名 Mongo表名不带dbName
  393. * @access public
  394. * @return string
  395. */
  396. public function getTableName()
  397. {
  398. if (empty($this->trueTableName)) {
  399. $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : '';
  400. if (!empty($this->tableName)) {
  401. $tableName .= $this->tableName;
  402. } else {
  403. $tableName .= parse_name($this->name);
  404. }
  405. $this->trueTableName = strtolower($tableName);
  406. }
  407. return $this->trueTableName;
  408. }
  409. /**
  410. * 分组查询
  411. * @access public
  412. * @return string
  413. */
  414. public function group($key, $init, $reduce, $option = array())
  415. {
  416. $option = $this->_parseOptions($option);
  417. //合并查询条件
  418. if (isset($option['where'])) {
  419. $option['condition'] = array_merge((array) $option['condition'], $option['where']);
  420. }
  421. return $this->db->group($key, $init, $reduce, $option);
  422. }
  423. /**
  424. * 返回Mongo运行错误信息
  425. * @access public
  426. * @return json
  427. */
  428. public function getLastError()
  429. {
  430. return $this->db->command(array('getLastError' => 1));
  431. }
  432. /**
  433. * 返回指定集合的统计信息,包括数据大小、已分配的存储空间和索引的大小
  434. * @access public
  435. * @return json
  436. */
  437. public function status()
  438. {
  439. $option = $this->_parseOptions();
  440. return $this->db->command(array('collStats' => $option['table']));
  441. }
  442. /**
  443. * 取得当前数据库的对象
  444. * @access public
  445. * @return object
  446. */
  447. public function getDB()
  448. {
  449. return $this->db->getDB();
  450. }
  451. /**
  452. * 取得集合对象,可以进行创建索引等查询
  453. * @access public
  454. * @return object
  455. */
  456. public function getCollection()
  457. {
  458. return $this->db->getCollection();
  459. }
  460. }