<?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