dongxuan1314 2013-04-17 19:16
浏览 36
已采纳

我可以通过传递对Mongo连接的引用从远程类连接到MongoDB吗?

I'm using Kohana's php framework (2.x) and I want to have a helper that lets me access Mongo through a function and return the results as an object. For this to actually query the database, do I have to connect to the database in the local class? Or can I pass $this->mongo->collection from a connected object? I'm sure I have errors and misconceptions in my code so feel free to point that out.

class mongoaccess
{   

function __construct()
{     
        try
        {
            //$m = new MongoClient();       // not sure how to go about the connection
            //$this->mongo = Mongo_Database::instance();
        }
            catch (Exception $e) 
            {
                error_log('Caught exception: ' . $e->getMessage() . "
");
            }
}

    /** 
    * Converts an array to an object. 
    * Multiple nesting supported.
    * @param $array | type: array | Ex: array('newvariable' => 'value')
    */
public static function arrayToObject($array) 
    {
        $object = new stdClass;
        foreach($array as $key => $value) 
        {
            if(is_array($value)) 
            {           
                $object->{$key} = arrayToObject($value); //Recursive
            } else {$object->{$key} = $value;}
        }
      return $object;
    } 



    /** Provides easy access to findOne()
     * @param $collection | type: object member | Ex: $this->mongo->usersinapps
     * @param $query | type: array | Ex: array('keyy' => 'valuee')  
     */
public static function getObjectFromMongo($collection, $query) 
{
    if (empty($query) || !is_array($query) || empty($collection))
        {return null;}

     $collection->findOne($query);

             $object = new stdClass();
     $object = mongoaccess::arrayToObject($query);

     return $object;
}

    // needs review
    /** Provides easy access to find() - iterates through cursor and returns array of objects
     * @param $collection | type: object member | Ex: $this->mongo->usersinapps
     * @param $query | type: array | Ex: array('keyy' => 'valuee')  
     */
public static function getObjectsFromMongo($collection, $query)
{
    if (empty($query) || !is_array($query) || empty($collection))
        {return null;}

    $objectArray = array();

    $mongoResultsCursor = $collection->find($query);

     while ($mongoResultsCursor->hasNext())
     {
        $document = $mongoResultsCursor->getNext();
        array_push($objectArray, $document);
     }

    $object = mongoaccess::arrayToObject($objectArray);

    return $object;
}

}

  • 写回答

1条回答 默认 最新

  • dongxin2734 2013-04-17 20:03
    关注
    class Mongoaccess
    {   
        protected $_mongo;
    
        function __construct()
        {
            $database     = 'test';
            $connect      = new MongoClient('mongodb://localhost:27017/'.$database);
            $this->_mongo = $connect->{$database}; // $this->_mongo
        }
    
        function get($collection, $criteria)
        {
            $data = $this->get_many($collection, $criteria);
    
            return ($data) ? reset($data) : $data; // Get first array
        }
    
        function get_many($collection, $criteria, $findOne = FALSE)
        {
            $find      = $this->_mongo->{$collection}->find($criteria);
    
            // find().limit(1) faster than findOne()
            // http://dba.stackexchange.com/a/7587
            $find      = $findOne ? $find->limit(1) : $find;
    
            // Array to object
            // https://coderwall.com/p/k-bwtq
            $data      = json_encode(iterator_to_array($find));
            $to_object = json_decode($data);
    
            return $to_object;
        }
    }
    

    Mongoaccess::get('collection', $criteria_array);
    Mongoaccess::get_many('collection', $criteria_array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大