<?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. fir.im Weekly - 这是一份强大的 SwiftGuide

    大新闻!Apple 10 亿美元融资滴滴!库克大叔对中国 iOS 开发者表达了高度认可,同时也传出 iOS 10 将内置滴滴 App 的消息.想像下,某个加班的深夜飙完代码,最性感的事情莫过于:「Si ...

  2. 发布iOS应用程序到苹果APP STORE完整流程

    参考:http://blog.csdn.net/mad1989/article/details/8167529(xcode APP 打包以及提交apple审核详细流程(新版本更新提交审核)) http ...

  3. cordova 版本

    cordova 版本 npm install cordova@xxxx https://travis-ci.org/apache/cordova-cli https://www.npmjs.com/p ...

  4. Java多线程synchronized同步

    非线程安全问题 “非线程安全”问题存在于“实例变量”中,如果是方法内部的私有变量,则不存在“非线程问题”.也即是说,方法中的变量永远是线程安全的. 如果多个线程共同访问1个对象中的实例变量,则可能线程 ...

  5. OpenCascade Primitives BRep - Box

    OpenCascade Primitives BRep - Box eryar@163.com Abstract. BRep is short for Boundary Representation. ...

  6. Hadoop学习笔记—MapReduce的理解

    我不喜欢照搬书上的东西,我觉得那样写个blog没多大意义,不如直接把那本书那一页告诉大家,来得省事.我喜欢将我自己的理解.所以我会说说我对于Hadoop对大量数据进行处理的理解.如果有理解不对欢迎批评 ...

  7. CNN 笔记3

  8. Struts2 源码分析——调结者(Dispatcher)之准备工作

    章节简言 上一章笔者讲到关于struts2过滤器(Filter)的知识.让我们了解到StrutsPrepareFilter和StrutsExecuteFilter的作用.特别是StrutsPrepar ...

  9. 安装thrift

    要求 thrift至少需要支持三种语言: Java PHP Go 预安装 基本教程: http://thrift.apache.org/docs/install/centos 使用最新的thrift, ...

  10. .NET魔法堂:工程构建基石->MSBuild

    一.前言 MSBuild是一个既熟悉又陌生的名字,Visual Studio的项目加载和构建均通过MSBuild来实现.VS中右键打开项目菜单,对应MSBuild的Build目标,对应MSBuild的 ...