PHP mongodb AR

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.