• PHP mongodb AR


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

  • 相关阅读:
    31、状态模式(详解版)
    33、中介者模式(详解版)
    36、备忘录模式(详解版)
    34、迭代器模式(详解版)
    30、责任链模式(职责链模式)详解
    29、命令模式(详解版)
    32、观察者模式(Observer模式)详解
    37、解释器模式(详解版)
    35、访问者模式(Visitor模式)详解
    28、策略模式(策略设计模式)详解
  • 原文地址:https://www.cnblogs.com/glory-jzx/p/5396065.html
Copyright © 2020-2023  润新知