From e740ae70000a6eccce7fbb875e29b968ed3cc786 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Tue, 15 Nov 2011 19:19:58 +0400 Subject: [PATCH] MongoModel class created --- model/MongoModel.php | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 model/MongoModel.php diff --git a/model/MongoModel.php b/model/MongoModel.php new file mode 100644 index 0000000..02fc56e --- /dev/null +++ b/model/MongoModel.php @@ -0,0 +1,78 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Model + * @since 2011-11-15 + */ + +abstract class MongoModel extends Model +{ + + public function find($condition) + { + return $this->db->find($this->table(), $condition); + } + + protected function order(MongoStatement $cursor, $sort = array()) + { + return $cursor->sort($sort); + } + + protected function skip(MongoStatement $cursor, $skip = 0) + { + return $cursor->skip($skip); + } + + protected function limit(MongoStatement $cursor, $limit = 0) + { + return $cursor->limit($limit); + } + + public function get($id) + { + return $this->db->get($this->table(), array('_id' => $id))->fetch(); + } + + public function delete($id) + { + return $this->db->delete($this->table(), array('id' => $id)); + } + + protected function fetchField($data, $params = array(), $field, $cache_key = null) + { + if (!$cache_key || !$result = $cache_key->get()) { + $result = $this->db->find($this->table(), $data, array($field => 1))->fetchField($field); + if ($cache_key) { + $cache_key->set($result); + } + } + return $result; + } + + protected function fetch($data, $params = array(), $cache_key = null) + { + if (!$cache_key || !$result = $cache_key->get()) { + $result = $this->db->find($this->table(), $data)->fetch(); + if ($cache_key) { + $cache_key->set($result); + } + } + return $result; + } + + protected function fetchAll($data, $params = array(), $cache_key = null) + { + if (!$cache_key || !$result = $cache_key->get()) { + $result = $this->db->find($this->table(), $data)->fetchAll(); + if ($cache_key) { + $cache_key->set($result); + } + } + return $result; + } +} \ No newline at end of file