Files
majestic/model/MongoModel.php

95 lines
2.4 KiB
PHP
Raw Normal View History

2011-11-15 19:19:58 +04:00
<?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())
2011-11-15 19:19:58 +04:00
{
return $this->db->find($this->table(), $condition, $fields);
2011-11-15 19:19:58 +04:00
}
public function get($id)
{
2012-01-18 23:30:34 +04:00
if ($this->useMongoId) {
if (!$id instanceof MongoId) {
$id = new MongoId($id);
}
}
2011-11-15 19:19:58 +04:00
return $this->db->get($this->table(), array('_id' => $id))->fetch();
}
2012-01-18 23:30:34 +04:00
public function batchInsert($data)
{
return $this->db->insert($this->table(), $data, true);
}
2011-11-15 19:19:58 +04:00
public function delete($id)
{
2012-01-18 23:30:34 +04:00
if ($this->useMongoId) {
if (!$id instanceof MongoId) {
$id = new MongoId($id);
}
}
2011-11-17 15:12:47 +04:00
return $this->db->delete($this->table(), array('_id' => $id));
2011-11-15 19:19:58 +04:00
}
public function deleteAll($query = array())
{
$this->db->delete($this->table(), $query);
}
2011-12-06 14:16:13 +04:00
2011-11-15 19:19:58 +04:00
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;
}
}