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.

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