Browse Source

MongoModel class created

master
Anton Grebnev 13 years ago
parent
commit
e740ae7000
  1. 78
      model/MongoModel.php

78
model/MongoModel.php

@ -0,0 +1,78 @@
<?php
/**
* Класс модели данных
*
* @copyright NetMonsters <team@netmonsters.ru>
* @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;
}
}
Loading…
Cancel
Save