<?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. 快速入门系列--WCF--06并发限流、可靠会话和队列服务

    这部分将介绍一些相对深入的知识点,包括通过并发限流来保证服务的可用性,通过可靠会话机制保证会话信息的可靠性,通过队列服务来解耦客户端和服务端,提高系统的可服务数量并可以起到削峰的作用,最后还会对之前的 ...

  2. seajs模块化开发

    seajs是一个起辅助作用的库,所以它可以更方便开发,而它可以解决以下问题: 1.命名问题,就是冲突 2.性能问题,就是只要一个功能,但却使用一个大插件中的一个小功能,所以要手动拆分出这个功能 3.j ...

  3. java持久层框架mybatis如何防止sql注入

    看到一篇很好的文章:http://www.jfox.info/ava-persistence-framework-mybatis-how-to-prevent-sql-injection sql注入大 ...

  4. PHP获取图片宽度高度、大小尺寸、图片类型、用于布局的img属性

    //php自带函数 getimagesize()$img_info = getimagesize('tomener.jpg'); echo '<pre>'; print_r($img_in ...

  5. 使用office制作图章公章

    制作公章的软件非常多,随便到网上一搜就有成千成百的软件或小工具,常用的有PS.Coreldraw.Ai.Word等,拥有一款office可以使用word来制作,方法挺简单,功能挺强大.寥寥数笔难以形容 ...

  6. 从css3书写顺序引出来的border-radius参数

    本鱼表示偶已经不会取标题了... 当时写这篇文章主要是想探讨一下优雅降级和渐进增强的区别,按照正常的逻辑思维,不管是降级还是增强,应该对于效果是没什么区别的,因为后者会覆盖前者,但今天无意看到张鑫旭的 ...

  7. Clank – 快速构建移动 APP 原型的 HTML/CSS 框架

    Clank 是一个自由和开放源码的 HTML/CSS 框架,可以帮助你轻松创建本地手机或平板电脑应用程序的原型.它着重于提供一致的用户界面,不会刻意去模仿一个特定的 OS .该框架是基于组件的,这样我 ...

  8. Asp.Net工作原理

    1. ASP.NET页面的的一般处理过程: 客户请求WEB页面 WEB服务寻找指令文件(.ASPX) ASP.NET代码被发送给CLR进行编译 HTML流返回给浏览器和指令 浏览器处理HTML并显示页 ...

  9. 50款免费 PSD 名片设计模板源文件下载《下篇》

    名片是陌生人之间建立联系的最便捷.最有效的工具.名片它可能是给你的客户留下正面的印象第一步,另一方面,名片是一个企业最重要和最符合成本效益的营销工具之一,尤其是对于刚刚起步的企业.这里收集了50款免费 ...

  10. SQL Server代理(10/12):使用代理账号运行作业

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 在这一系列的上一篇,你查看了msdb库下用 ...