• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-26) 20次浏览 已收录 0个评论

MongoCursor Object
游标类

Mongo
Config.php配置文件
Table.php(mongodb操作数据库类文件)

Config.php配置文件

<?php<BR>require_once 'Zend/Exception.php';<BR>class Hrs_Mongo_Config<BR>{<BR>    const VERSION = '1.7.0';<BR>    const DEFAULT_HOST = 'localhost';<BR>    const DEFAULT_PORT = 27017;<BR>    private static $host = self::DEFAULT_HOST ;<BR>    private static $port = self::DEFAULT_PORT ;<BR>    private static $options = array(<BR>            'connect' => true,<BR>            'timeout' => 30,<BR>            //'replicaSet' => '' //If this is given, the master will be determined by using the ismaster database command on the seeds<BR>    );<BR>    public static $conn = '';<BR>    public static $defaultDb = '';<BR>    public static $linkStatus = '';<BR> <p>4本文¥来源gao!%daima.com搞$代*!码$网9</p><pre>搞代gaodaima码

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<BR>require_once 'Hrs/Mongo/Config.php';<BR>abstract class Hrs_Mongo_Table<BR>{<BR>    protected $_db = '';<BR>    protected $_name = '';<BR>    protected $_data = array();<BR>    protected $c_options = array(<BR>            'fsync'=>true,<BR>            'safe'=>true<BR>    );<BR>    protected $u_options = array(<BR>    //'upsert'=>false,<BR>            'multiple'=>true,<BR>            'fsync'=>true,<BR>            'safe'=>true<BR>    );<BR>    /*<BR>     protected $r_options = array(<BR>     );*/<BR>    protected $d_options = array(<BR>            'fsync'=>true,<BR>            'justOne'=>false,<BR>            'safe'=>true<BR>    );<BR>    protected function _setAdapter($database=''){<BR>        if(!$database)<BR>            throw new Zend_Exception('Mongodb数据库名称不能为空');<BR>        Hrs_Mongo_Config::selectDB($database);<BR>    }<BR>    public function __construct() {<BR>        if(Hrs_Mongo_Config::$conn instanceof Mongo){<BR>            $name = $this->_name;<BR>            $defDb = Hrs_Mongo_Config::$defaultDb;<BR>            $this->_db = $defDb->$name;<BR>        }else{<BR>            throw new Zend_Exception('Mongodb服务器连接失败');<BR>        }<BR>    }<BR>    public function insert($data){<BR>        if(!$this->testLink()) return false;<BR>        $ret = $this->_db->insert($data, $this->c_options);<BR>        return $ret;<BR>    }<BR>    public function update($data, $where){<BR>        if(!$this->testLink()) return false;<BR>        return $this->_db->update($where, $data, $this->u_options);<BR>    }<BR>    public function find($where=array(),$limit=0){<BR>        if($this->testLink()) {<BR>            if($limit>0){<BR>                $this->_data = $where ? $this->_db->find($where)->limit($limit)->snapshot() : $this->_db->find()->limit($limit)->snapshot();<BR>            }else{<BR>                $this->_data = $where ? $this->_db->find($where)->limit($limit)->snapshot() : $this->_db->find()->limit($limit)->snapshot();<BR>            }<BR>        }<BR>        return $this;<BR>    }<BR>    //find cursor<BR>    /*<BR>     * 获取游标对象<BR>     */<BR>    public function look($where=array(),$fields=array()){<BR>        if($this->testLink()) {<BR>            if($fields){<BR>                return $where ? $this->_db->find($where,$fields): $this->_db->find()->fields($fields);<BR>            }else{<BR>                return $where ? $this->_db->find($where) : $this->_db->find();<BR>            }<BR>        }<BR>        return false;<BR>    }<BR>    public function delete($where){<BR>        if(!$this->testLink()) return false;<BR>        return $this->_db->remove($where, $this->d_options);<BR>    }<BR>    public function dropMe(){<BR>        if(!$this->testLink()) return false;<BR>        return $this->_db->drop();<BR>    }<BR>    public function __toString(){<BR>        return $this->_data;<BR>    }<BR>    public function toArray(){<BR>        $tmpData = array();<BR>        foreach($this->_data as $id=>$row){<BR>            $one_row = array();<BR>            foreach($row as $key=>$col){<BR>                $one_row[$key] = $col;<BR>            }<BR>            $one_row['_id'] = $id;<BR>            $tmpData[] = $one_row;<BR>        }<BR>        return $tmpData;<BR>    }<BR>    protected function testLink(){<BR>        return Hrs_Mongo_Config::$linkStatus == 'success' ? true :false;<BR>    }<BR>}<BR>


要点注意!!!
第一种方法

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


第二种方法

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


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


第二种需要的是find得到的不是数组
find($where)->getCursor();是MongoCursor Object

注意注意
find()返回的是当前对象
toArray()方法是把当前对象转换为数组
getCursor()方法是把当前对象转换为MongoCursor Object(游标对象)


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mongo Table类文件 获取MongoCursor(游标)的实现方法分析_php技巧

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址