<?PHP/** * @author Xiaojiang*/Abstract classmongoar{Private $db=NULL; Public function__construct () {$this->db =$this-Getcol (); } protected functionrules () {return Array(); } //Get Write link Public functiongetw () {return $this->db->W (); } //get Read link Public functionGetr () {return $this->db->R (); } Public functionCheckrule ($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=NewMongoId ($v); Break; Case' String ':default:$v= (string)$v; Break; } return $v; } Public functionFind$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 '] ); } returnIterator_to_array ($cursor ); } Public functionFindOne ($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 functionInsert$data,$opt ){ return $this->db->w ()->selectdb ($this->getdbname ())->selectcollection ($this->getcolname ())->insert ($data,$opt ); } Public functionUpdate$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*/classMongomodelextendsmongoar{ Public $PK= ' _id '; Private $attributes=Array(); protected $mongodb _col=NULL; Static $models=Array(); Static Public functionGetInstance ($name=__class__ ){ if( !isset(Self::$models[$name] ) Self::$models[$name] =New $name(); returnSelf::$models[$name]; } /** * Get current collection*/ protected functionGetcol () {}/** Get table name*/ protected functionGetcolname () {} Public functionSetAttributes (Array $arr ){ $this->attributes =$arr; return $this; } Public functionSave () {$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