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.

81 lines
2.1 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 count($query = array(), $limit = 0, $skip = 0)
  14. {
  15. return $this->db->count($this->table(), $query, $limit, $skip);
  16. }
  17. public function find($condition = array())
  18. {
  19. return $this->db->find($this->table(), $condition);
  20. }
  21. public function get($id)
  22. {
  23. return $this->db->get($this->table(), array('_id' => $id))->fetch();
  24. }
  25. public function delete($id)
  26. {
  27. return $this->db->delete($this->table(), array('_id' => $id));
  28. }
  29. /**
  30. * @TODO: test method
  31. */
  32. public function deleteAll($query = array())
  33. {
  34. $this->db->delete($this->table(), $query);
  35. }
  36. /**
  37. * @TODO: check for limits (if just one record needed)
  38. */
  39. protected function fetchField($data, $params = array(), $field, $cache_key = null)
  40. {
  41. if (!$cache_key || !$result = $cache_key->get()) {
  42. $result = $this->db->find($this->table(), $data, array($field => 1))->fetchField($field);
  43. if ($cache_key) {
  44. $cache_key->set($result);
  45. }
  46. }
  47. return $result;
  48. }
  49. /**
  50. * @TODO: check for limits (if just one record needed)
  51. */
  52. protected function fetch($data, $params = array(), $cache_key = null)
  53. {
  54. if (!$cache_key || !$result = $cache_key->get()) {
  55. $result = $this->db->find($this->table(), $data)->fetch();
  56. if ($cache_key) {
  57. $cache_key->set($result);
  58. }
  59. }
  60. return $result;
  61. }
  62. protected function fetchAll($data, $params = array(), $cache_key = null)
  63. {
  64. if (!$cache_key || !$result = $cache_key->get()) {
  65. $result = $this->db->find($this->table(), $data)->fetchAll();
  66. if ($cache_key) {
  67. $cache_key->set($result);
  68. }
  69. }
  70. return $result;
  71. }
  72. }