<?php
/**
* @author xiaojiang
*/
abstract class MongoAr{ private $db = null; public function __construct(){
$this->db = $this->getCol();
} protected function rules(){
return array();
} //获取写链接
public function getW(){
return $this->db->w();
}
//获取读链接
public function getR(){
return $this->db->r();
} public function checkRule( $w ){ $rules = $this->rules();
if( !$rules )
return $w;
foreach( $w as $k => &$v ){
foreach ( $this->rules() as $r ){
if( strpos( $r[0], $k ) !== false ){
$v = $this->setType( $v ,$r[1] );
}
}
}
return $w;
} /**
* change type by use of force.
* @param unknown $v
* @param unknown $type
*/
public function setType( $v, $type ){
switch ( $type ){
case 'int':
$v = (int)$v;
break;
case 'object_id':
$v = new MongoId($v);
break;
case 'string':
default:
$v = (string)$v;
break;
}
return $v;
} public function find( $where, $option = array() ){
$where = $this->checkRule( $where );
$_fields = array();
if( !empty( $option['fields'] ) ){
$_fields = explode(',', $option['fields']);
}
$cursor = $this->db->r()->selectDb( $this->getDbName() )->selectCollection( $this->getColName() )->find( $where ,$_fields ); if( !empty( $option['sort'] ) ){
$cursor = $cursor->sort( $option['sort'] );
}
if( !empty( $option['page'] ) ){
$option['offset'] = ( $option['page'] -1 ) * $option['page_size'];
$option['limit'] = $option['page_size'];
}
if( !empty( $option['limit'] ) ){
if( !empty( $option['offset'] ) ){
$cursor = $cursor->skip( $option['offset'] );
}
$cursor = $cursor->limit( (int)$option['limit'] );
}
return iterator_to_array( $cursor );
} public function findOne( $where ){
$where = $this->checkRule( $where );
$_fields = array();
if( !empty( $option['fields'] ) ){
$_fields = explode(',', $option['fields']);
}
return $this->db->r()->selectDb( $this->getDbName() )->selectCollection( $this->getColName() )->findOne( $where, $_fields );
} public function insert( $data, $opt ){
return $this->db->w()->selectDb( $this->getDbName() )->selectCollection( $this->getColName() )->insert( $data, $opt );
} public function update( $where, $data ,$opt = array() ){
$ret = $this->db->w()->selectDb( $this->getDbName() )->selectCollection( $this->getColName() )->update( $where, $data, $opt );
return $ret;
}
} ?>

MongoAr.php

<?php
/**
* @author jiangzaixing
*/
class MongoModel extends MongoAr{ public $pk = '_id';
private $attributes = array();
protected $mongodb_col = null;
static $models = array();
static public function getInstance( $name = __CLASS__ ){
if( !isset(self::$models[$name]) )
self::$models[$name] = new $name();
return self::$models[$name];
} /**
* 获取当前collection
*/
protected function getCol(){} /**
* get table name
*/
protected function getColName(){} public function setAttributes( array $arr ){
$this->attributes = $arr;
return $this;
} public function save(){ $pValue = ( isset( $this->attributes[$this->pk] ) && !empty( $this->attributes[$this->pk] ) ) ? $this->attributes[$this->pk] : '';
if( empty( $pValue ) ){
$ret = $this->insert( $this->attributes );
}else{
$this->attributes = $this->checkRule( $this->attributes );
$where[$this->pk] = $this->attributes[$this->pk];
unset( $this->attributes[$this->pk] );
$ret = $this->update( $where , array('$set'=>$this->attributes ) );
}
return $ret;
} } ?>

MongoModel.php

PHP mongodb AR的更多相关文章

  1. MongoDB基础

    1.概念及特点 说明:由于部分语句中$ 符号无法正常显示,使用¥代表 概念 MongoDB是一个基于文档的分布式的开源的NoSQL数据库,文档的结构为BSON形式,每一个文档都有一个唯一的Object ...

  2. MySQL、MongoDB、Redis数据库Docker镜像制作

    MySQL.MongoDB.Redis数据库Docker镜像制作 在多台主机上进行数据库部署时,如果使用传统的MySQL的交互式的安装方式将会重复很多遍.如果做成镜像,那么我们只需要make once ...

  3. 谈谈php里的DAO Model AR

    这次要谈的3个关键字:DAO.Model.AR,是我们在做web应用时常见的几个概念,也被称作设计模式(design pattern),先简单看看它们的全拼和中文: DAO:Data Access O ...

  4. MongoDB性能优化

    一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引 ...

  5. MongoDB性能篇之创建索引,组合索引,唯一索引,删除索引和explain执行计划

    这篇文章主要介绍了MongoDB性能篇之创建索引,组合索引,唯一索引,删除索引和explain执行计划的相关资料,需要的朋友可以参考下 一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存 ...

  6. MongoDB 常用故障排查工具

    1.profile profiling levels: 0,关闭profile:1,只抓取slow查询:2,抓取所有数据. 启动profile并且设置Profile级别: 可以通过mongo shel ...

  7. 为MongoDB创建一个Windows服务

    一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...

  8. MongoDB 3.0 新特性【转】

    本文来自:http://www.open-open.com/lib/view/open1427078982824.html#_label3 更多信息见官网: http://docs.mongodb.o ...

  9. MongoDB 分片管理(不定时更新)

    背景: 通过上一篇的 MongoDB 分片的原理.搭建.应用 大致了解了MongoDB分片的安装和一些基本的使用情况,现在来说明下如何管理和优化MongoDB分片的使用. 知识点: 1) 分片的配置和 ...

随机推荐

  1. SpringMVC与mybatis整合

    一.逆向工程生成基础信息 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generator ...

  2. Test Design Guidelines for Reusability

    Last Updated: JAN.10.2008 From: http://safsdev.sourceforge.net/sqabasic2000/TestDesignGuidelines.htm ...

  3. 简单的跨平台c/c++日志记录

    CLog.h #include <stdlib.h> #pragma once #ifndef _CLOG #define _CLOG #define CLOG_DEBUG 0 #defi ...

  4. CSS中模拟父元素选择器

    很多情况下,我们需要找到父元素,但可惜的是css中并没有这样的一个选择器. 至于原因可以看张鑫旭的如何在CSS中实现父选择器效果这篇文章. 简单来说这个实现并不是真正的父元素选择器,只是利用其它思路来 ...

  5. Androd开发之广告栏设计

    对于做Android开发的工程师对于这个效果的实现一定不陌生,本篇我将带领大家先简单实现这个效果,再为大家介绍一下其中的原理,方便新手学习,老手复习,内容简单易懂,没有基础一样学习,不扯没用的了,下面 ...

  6. 【转载】经典SQL语句大全

    [原文地址]http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html 一.基础 1.说明:创建数据库CREATE DATABAS ...

  7. List和Dictionary泛型类查找效率浅析

    List和Dictionary泛型类查找效率存在巨大差异,前段时间亲历了一次.事情的背景是开发一个匹配程序,将书籍(BookID)推荐给网友(UserID),生成今日推荐数据时,有条规则是同一书籍七日 ...

  8. 开发笔记:用Owin Host实现脱离IIS跑Web API单元测试

    今天在开发一个ASP.NET Web API项目写单元测试时,实在无法忍受之前的笨方法,决定改过自新. 之前Web API的单元测试需要进行以下的操作: 初始配置: 1)在IIS中创建一个站点指定We ...

  9. Redis在win7上的可视化应用

    Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  10. IOS开发初步

    由于工程实践项目的原因,得学习下IOS开发,今天才知道苹果09年才出的开发工具和开发包,也就是说,满打满算,现在顶多有5年IOS开发的工作经验.在我国2010年才火起来,因为那时候国内的iphone4 ...