PHP MongoDB AR

Source: Internet
Author: User
Tags explode findone php mongodb

<?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-&GT;PK]) &&!Empty($this->attributes[$this-&GT;PK])?$this->attributes[$this-&GT;PK]: "; if(Empty($pValue ) ){            $ret=$this->insert ($this-attributes); }Else{            $this->attributes =$this->checkrule ($this-attributes); $where[$this-&GT;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.