• 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

  • 相关阅读:
    python读取二进制文件写入到txt
    python格式化输出
    字符编码
    python--随时记录
    python-web服务器
    openssh移植
    select、poll、epoll
    (总结)Nginx/LVS/HAProxy负载均衡软件的优缺点详解
    heartbeat与keepalived的区别
    salt 常用命令整理
  • 原文地址:https://www.cnblogs.com/glory-jzx/p/5396065.html
Copyright © 2020-2023  润新知