You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

  1. <?php
  2. /**
  3. * Класс модели данных
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  6. * @link http://netmonsters.ru
  7. * @package Majestic
  8. * @subpackage Model
  9. * @since 2011-11-15
  10. */
  11. abstract class MongoModel extends Model
  12. {
  13. public function find($condition)
  14. {
  15. return $this->db->find($this->table(), $condition);
  16. }
  17. protected function order(MongoStatement $cursor, $sort = array())
  18. {
  19. return $cursor->sort($sort);
  20. }
  21. protected function skip(MongoStatement $cursor, $skip = 0)
  22. {
  23. return $cursor->skip($skip);
  24. }
  25. protected function limit(MongoStatement $cursor, $limit = 0)
  26. {
  27. return $cursor->limit($limit);
  28. }
  29. public function get($id)
  30. {
  31. return $this->db->get($this->table(), array('_id' => $id))->fetch();
  32. }
  33. public function delete($id)
  34. {
  35. return $this->db->delete($this->table(), array('id' => $id));
  36. }
  37. protected function fetchField($data, $params = array(), $field, $cache_key = null)
  38. {
  39. if (!$cache_key || !$result = $cache_key->get()) {
  40. $result = $this->db->find($this->table(), $data, array($field => 1))->fetchField($field);
  41. if ($cache_key) {
  42. $cache_key->set($result);
  43. }
  44. }
  45. return $result;
  46. }
  47. protected function fetch($data, $params = array(), $cache_key = null)
  48. {
  49. if (!$cache_key || !$result = $cache_key->get()) {
  50. $result = $this->db->find($this->table(), $data)->fetch();
  51. if ($cache_key) {
  52. $cache_key->set($result);
  53. }
  54. }
  55. return $result;
  56. }
  57. protected function fetchAll($data, $params = array(), $cache_key = null)
  58. {
  59. if (!$cache_key || !$result = $cache_key->get()) {
  60. $result = $this->db->find($this->table(), $data)->fetchAll();
  61. if ($cache_key) {
  62. $cache_key->set($result);
  63. }
  64. }
  65. return $result;
  66. }
  67. }