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.

219 lines
6.2 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. /**
  12. * @property MongoDriver $db
  13. */
  14. abstract class MongoModel extends Model
  15. {
  16. /**
  17. * @var string Mongo id field to use as key
  18. */
  19. protected $key = '_id';
  20. /**
  21. * @var bool Is Mongo key field uses original MongoId
  22. */
  23. protected $useMongoId = true;
  24. public function __construct($connection = 'default')
  25. {
  26. parent::__construct($connection);
  27. }
  28. protected function count($query = array(), $limit = 0, $skip = 0)
  29. {
  30. return $this->db->count($this->table(), $query, $limit, $skip);
  31. }
  32. public function get($id)
  33. {
  34. if ($this->useMongoId) {
  35. if (!$id instanceof MongoId) {
  36. $id = new MongoId($id);
  37. }
  38. }
  39. return $this->fetch(array($this->key => $id));
  40. }
  41. public function batchInsert($data)
  42. {
  43. return $this->db->insert($this->table(), $data, true);
  44. }
  45. public function update($data, $where)
  46. {
  47. if ($this->useMongoId) {
  48. if (!$where instanceof MongoId) {
  49. $where = new MongoId($where);
  50. }
  51. }
  52. return $this->db->update($this->table(), $data, array($this->key => $where));
  53. }
  54. public function delete($id)
  55. {
  56. if ($this->useMongoId) {
  57. if (!$id instanceof MongoId) {
  58. $id = new MongoId($id);
  59. }
  60. }
  61. return $this->db->delete($this->table(), array($this->key => $id));
  62. }
  63. /**
  64. * @param array $data Request
  65. * @param array $params Request parameters
  66. * @param string $field Requested field name
  67. * @param CacheKey $cache_key Key for caching in
  68. * @return mixed
  69. */
  70. protected function fetchField($data, $params = array(), $field, $cache_key = null)
  71. {
  72. if (!$cache_key || !$result = $cache_key->get()) {
  73. $result = $this->db
  74. ->find($this->table(), $data, array($field => 1))
  75. ->limit($this->getLimit($params))
  76. ->skip($this->getSkip($params))
  77. ->order($this->getOrder($params))
  78. ->fetchField($field);
  79. if ($cache_key) {
  80. $cache_key->set($result);
  81. }
  82. }
  83. return $result;
  84. }
  85. /**
  86. * @param array $data Request
  87. * @param array $params Request parameters
  88. * @param CacheKey $cache_key Key for caching in
  89. * @return mixed
  90. */
  91. protected function fetch($data, $params = array(), $cache_key = null)
  92. {
  93. if (!$cache_key || !$result = $cache_key->get()) {
  94. $result = $this->db
  95. ->find($this->table(), $data, $this->getFields($params))
  96. ->limit(1)
  97. ->skip($this->getSkip($params))
  98. ->order($this->getOrder($params))
  99. ->fetch();
  100. if ($cache_key) {
  101. $cache_key->set($result);
  102. }
  103. }
  104. return $result;
  105. }
  106. /**
  107. * @param array $data
  108. * @param array $params
  109. * @param CacheKey $cache_key
  110. * @return array
  111. */
  112. protected function fetchAll($data, $params = array(), $cache_key = null)
  113. {
  114. if (!$cache_key || !$result = $cache_key->get()) {
  115. $result = $this->db
  116. ->find($this->table(), $data, $this->getFields($params))
  117. ->limit($this->getLimit($params))
  118. ->skip($this->getSkip($params))
  119. ->order($this->getOrder($params))
  120. ->fetchAll();
  121. if ($cache_key) {
  122. $cache_key->set($result);
  123. }
  124. }
  125. return $result;
  126. }
  127. /**
  128. * @param array $params Parameters for find query
  129. * @return int Query result limits rule
  130. * */
  131. private function getLimit($params = array())
  132. {
  133. return isset($params['limit']) ? (int) $params['limit'] : 0;
  134. }
  135. /**
  136. * @param array $params Parameters for find query
  137. * @return int Query result skip rule
  138. * */
  139. private function getSkip($params = array())
  140. {
  141. return isset($params['skip']) ? (int) $params['skip'] : 0;
  142. }
  143. /**
  144. * @param array $params Parameters for find query
  145. * @return array Query result sort rules
  146. * @throws GeneralException
  147. * */
  148. private function getOrder($params = array())
  149. {
  150. $order = array();
  151. if (isset($params['order'])) {
  152. if (is_string($params['order'])) {
  153. $order = array($params['order'] => 1);
  154. } elseif (is_array($params['order'])) {
  155. $order = $params['order'];
  156. } else {
  157. throw new GeneralException('Wrong order parameter given to query.');
  158. }
  159. }
  160. return $order;
  161. }
  162. /**
  163. * @param array $params Parameters for find query
  164. * @return array Query result sort rules
  165. * @throws GeneralException
  166. * */
  167. private function getFields($params)
  168. {
  169. $fields = array();
  170. if (isset($params['fields'])) {
  171. if (is_string($params['fields'])) {
  172. $fields = array($params['fields'] => 1);
  173. } elseif (is_array($params['fields'])) {
  174. if (array_keys($params['fields']) === range(0, count($params['fields']) - 1)) {
  175. foreach ($params['fields'] as $filed) {
  176. $fields[$filed] = 1;
  177. }
  178. } else {
  179. $fields = $params['fields'];
  180. }
  181. } else {
  182. throw new GeneralException('Wrong fields parameter given to query.');
  183. }
  184. }
  185. return $fields;
  186. }
  187. /**
  188. * Sets the additional condition if it is set
  189. *
  190. * @param array $query Initial query
  191. * @param string $filed Field name to set
  192. * @param mixed $value Field value to set
  193. * @return MongoModel Current model object
  194. */
  195. protected function addCondition(&$query, $filed, $value)
  196. {
  197. if(!is_null($value) && $value !== false) {
  198. $query[$filed] = $value;
  199. }
  200. return $this;
  201. }
  202. }