<?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 - 从 iOS 10 SDK 新特性说起

    从 iOS 7 翻天覆地的全新设计,iOS 8 中 Size Classes 的出现,应用扩展,以及 Cloud Kit 的加入,iOS 9 的分屏多任务特性,今年的 WWDC iOS 10 SDK ...

  2. html5 浏览器端数据库

    为什么使用浏览器端数据库:随着浏览器的处理能力不断增强,越来越多的网站开始考虑,将大量数据储存在客户端,这样可以减少用户等待从服务器获取数据的时间. 一.localStorage  — 本地存储  可 ...

  3. SQL Server 错误日志收缩(ERRORLOG)

    一.基础知识 默认情况下,错误日志位于 : C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\ERRORLOG 和ERRORLOG.n 文 ...

  4. 数据类型,隐式转换以及json,对象,引用类型,预解析 视频教程

    随便看看,需要有一点一点基础. 链接:http://pan.baidu.com/s/1c20pcOC 密码:xq2x

  5. 【原创】Matlab.NET混合编程技巧之找出Matlab内置函数

                  本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新    Matlab和C#混合编程文章目录 :[目录]Matlab和C#混合编程文章目录 Matlab与.N ...

  6. 用DropDownList实现的省市级三级联动

    这是一个用DropDownList 实现的省市级三级联动,记录一下········ <asp:ScriptManager ID="ScriptManager1" runat= ...

  7. Spring MVC 学习总结(六)——Spring+Spring MVC+MyBatis框架集成

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

  8. Spark入门实战系列--2.Spark编译与部署(上)--基础环境搭建

    [注] 1.该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取: 2.Spark编译与部署将以CentOS 64位操作系统为基础,主要是考虑到实际应用 ...

  9. 【AngularJS】—— 12 独立作用域

    前面通过视频学习了解了指令的概念,这里学习一下指令中的作用域的相关内容. 通过独立作用域的不同绑定,可以实现更具适应性的自定义标签.借由不同的绑定规则绑定属性,从而定义出符合更多应用场景的标签. 本篇 ...

  10. Activity onDestroy() 回调缓慢问题分析及完美解决方案

    说到Acitivity的onDestroy,对所有的Android开发者都很熟悉,但是不一定都知道里面存在着一些坑,分享给大家,以后踩中的时候可以应急 用Activity的时候,会发现如下问题: 有时 ...