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.

95 lines
2.4 KiB

<?php
/**
* Класс модели данных
*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage Model
* @since 2011-11-15
*/
abstract class MongoModel extends Model
{
protected $useMongoId = true;
public function __construct($connection = 'default')
{
parent::__construct($connection);
}
public function count($query = array(), $limit = 0, $skip = 0)
{
return $this->db->count($this->table(), $query, $limit, $skip);
}
public function find($condition = array(), $fields = array())
{
return $this->db->find($this->table(), $condition, $fields);
}
public function get($id)
{
if ($this->useMongoId) {
if (!$id instanceof MongoId) {
$id = new MongoId($id);
}
}
return $this->db->get($this->table(), array('_id' => $id))->fetch();
}
public function batchInsert($data)
{
return $this->db->insert($this->table(), $data, true);
}
public function delete($id)
{
if ($this->useMongoId) {
if (!$id instanceof MongoId) {
$id = new MongoId($id);
}
}
return $this->db->delete($this->table(), array('_id' => $id));
}
public function deleteAll($query = array())
{
$this->db->delete($this->table(), $query);
}
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;
}
}