当前位置: 移动技术网 > IT编程>开发语言>PHP > mongo Table类文件 获取MongoCursor(游标)的实现方法分析

mongo Table类文件 获取MongoCursor(游标)的实现方法分析

2019年04月07日  | 移动技术网IT编程  | 我要评论

一千零一个愿望手语,7t9推广联盟,变频空调维修

mongocursor object
游标类

mongo
config.php配置文件
table.php(mongodb操作数据库类文件)

config.php配置文件
复制代码 代码如下:

<?php
require_once 'zend/exception.php';
class hrs_mongo_config
{
    const version = '1.7.0';
    const default_host = 'localhost';
    const default_port = 27017;
    private static $host = self::default_host ;
    private static $port = self::default_port ;
    private static $options = array(
            'connect' => true,
            'timeout' => 30,
            //'replicaset' => '' //if this is given, the master will be determined by using the ismaster database command on the seeds
    );
    public static $conn = '';
    public static $defaultdb = '';
    public static $linkstatus = '';
    public static function set($server = 'mongodb://localhost:27017', $options = array('connect' => true)) {
        if(!$server){
            $url = 'mongodb://'.self::$host.':'.self::$port;
        }
        if(is_array($server)){
            if(isset($server['host'])){
                self::$host = $server['host'];
            }
            if(isset($server['port'])){
                self::$port = $server['port'];
            }
            if(isset($server['user']) && isset($server['pass'])){
                $url = 'mongodb://'.$server['user'].':'.$server['pass'].'@'.self::$host.':'.self::$port;
            }else{
                $url = 'mongodb://'.self::$host.':'.self::$port;
            }
        }
        if(is_array($options)){
            foreach (self::$options as $o_k=>$o_v){
                if(isset($options[$o_k]))
                    self::$options[$o_k] = $o_v;
            }
        }
        try{                       
            self::$conn = new mongo($url, self::$options);
            self::$linkstatus = 'success';
        }catch (exception $e){
            self::$linkstatus = 'failed';
        }
        if(isset($server['database'])){
            self::selectdb($server['database']);
        }
    }
    public static function selectdb($database){
        if($database){
            try {
                if(self::$linkstatus=='success')
                    self::$defaultdb = self::$conn->selectdb($database);
                return self::$defaultdb;
            }
            catch(invalidargumentexception $e) {
                throw new zend_exception('mongodb数据库名称不正确');
            }
        }else{
            throw new zend_exception('mongodb数据库名称不能为空');
        }
    }
}

table.php(mongodb操作数据库类文件)
复制代码 代码如下:

<?php
require_once 'hrs/mongo/config.php';
abstract class hrs_mongo_table
{
    protected $_db = '';
    protected $_name = '';
    protected $_data = array();
    protected $c_options = array(
            'fsync'=>true,
            'safe'=>true
    );
    protected $u_options = array(
    //'upsert'=>false,
            'multiple'=>true,
            'fsync'=>true,
            'safe'=>true
    );
    /*
     protected $r_options = array(
     );*/
    protected $d_options = array(
            'fsync'=>true,
            'justone'=>false,
            'safe'=>true
    );
    protected function _setadapter($database=''){
        if(!$database)
            throw new zend_exception('mongodb数据库名称不能为空');
        hrs_mongo_config::selectdb($database);
    }
    public function __construct() {
        if(hrs_mongo_config::$conn instanceof mongo){
            $name = $this->_name;
            $defdb = hrs_mongo_config::$defaultdb;
            $this->_db = $defdb->$name;
        }else{
            throw new zend_exception('mongodb服务器连接失败');
        }
    }
    public function insert($data){
        if(!$this->testlink()) return false;
        $ret = $this->_db->insert($data, $this->c_options);
        return $ret;
    }
    public function update($data, $where){
        if(!$this->testlink()) return false;
        return $this->_db->update($where, $data, $this->u_options);
    }
    public function find($where=array(),$limit=0){
        if($this->testlink()) {
            if($limit>0){
                $this->_data = $where ? $this->_db->find($where)->limit($limit)->snapshot() : $this->_db->find()->limit($limit)->snapshot();
            }else{
                $this->_data = $where ? $this->_db->find($where)->limit($limit)->snapshot() : $this->_db->find()->limit($limit)->snapshot();
            }
        }
        return $this;
    }
    //find cursor
    /*
     * 获取游标对象
     */
    public function look($where=array(),$fields=array()){
        if($this->testlink()) {
            if($fields){
                return $where ? $this->_db->find($where,$fields): $this->_db->find()->fields($fields);
            }else{
                return $where ? $this->_db->find($where) : $this->_db->find();
            }
        }
        return false;
    }
    public function delete($where){
        if(!$this->testlink()) return false;
        return $this->_db->remove($where, $this->d_options);
    }
    public function dropme(){
        if(!$this->testlink()) return false;
        return $this->_db->drop();
    }
    public function __tostring(){
        return $this->_data;
    }
    public function toarray(){
        $tmpdata = array();
        foreach($this->_data as $id=>$row){
            $one_row = array();
            foreach($row as $key=>$col){
                $one_row[$key] = $col;
            }
            $one_row['_id'] = $id;
            $tmpdata[] = $one_row;
        }
        return $tmpdata;
    }
    protected function testlink(){
        return hrs_mongo_config::$linkstatus == 'success' ? true :false;
    }
}

要点注意!!!
第一种方法
复制代码 代码如下:

    //find cursor
    /*
     * 获取游标对象
     */
    public function look($where=array(),$fields=array()){
        if($this->testlink()) {
            if($fields){
                return $where ? $this->_db->find($where,$fields): $this->_db->find()->fields($fields);
            }else{
                return $where ? $this->_db->find($where) : $this->_db->find();
            }
        }
        return false;
    }

第二种方法
复制代码 代码如下:

    public function find($where=array(),$field=array()){
        if($this->testlink()) {
            $this->_data = $this->_db->find($where,$field)->sort(array("_id" => -1));
        }
        return $this;
    }

复制代码 代码如下:

    /*
     * 获取游标对象
     */
    public function getcursor(){
     return $this->_data;
    }

第二种需要的是find得到的不是数组
find($where)->getcursor();是mongocursor object

注意注意
find()返回的是当前对象
toarray()方法是把当前对象转换为数组
getcursor()方法是把当前对象转换为mongocursor object(游标对象)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网