C
C
casey2010-10-13 14:04:20
PHP
casey, 2010-10-13 14:04:20

PHP - how to connect to Mongo on demand (deferred)?

The module for working with Mongo, which is included in the PHP distribution package, provides the ability to connect to the server immediately - allowing you to select a database and collection without establishing a connection. There is such a construction for this:

$mongo = new \Mongo (MONGO_SERVER, array ("connect" => false));

However, in order to perform any action with data, you must first call $mongo->connect ().

In this connection, the question arose - maybe there is already a ready-made solution, a certain layer for working with Mongo like Zend_Db or Rediska, where this has already been implemented?

It’s pointless to connect every time, I also don’t want to reinvent the wheel, I turned to the collective mind :-)

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
sedictor, 2010-10-15
@sedictor

class lazyLoad_mongo {
    private $loaded = false;
    private $mongo;
    public function init() {
        $this->mongo = new Mongo(MONGO_SERVER, array ("connect" => false));
        $this->loaded = true;
    }
    public function __call($name, array $arguments) {
        if (!$this->loaded) {
            $this->init();
        }
        return call_user_func_array(array($this->mongo, $name), $arguments);
    }
    public function __callStatic($name, array $arguments) {
        $this->__call($name, $arguments);
    }
    public function __get($name) {
        if (!$this->loaded) {
            $this->init();
        }
        return $this->mongo->$name;
    }
    public function __set($name, $value) {
        if (!$this->loaded) {
            $this->init();
        }
        return $this->mongo->$name = $value;
    }
    public function __isset($name) {
        if (!$this->loaded) {
            $this->init();
        }
        return isset($this->mongo->$name);
    }
    public function __unset($name) {
        if (!$this->loaded) {
            $this->init();
        }
        return unset($this->mongo->$name);
    }
}

O
Oleksandr Brichuk, 2010-10-13
@aleksandro

Under Zend, there seems to be nothing except for the backend for the cache.

A
Arvid Godyuk, 2010-10-15
@Psih

Do a Lazy Load.
The general pattern is

function makeRequest()
{
    if (!$this->isConnected()) {
       $this->connect();
    }
    parent::makeQuery();
}

And you will have a delayed connection. This is how I work with MySQL. Implementation details are up to you :)

S
sedictor, 2010-10-15
@sedictor

Rewrote a little:

<?php
class Mongo_LazyLoad {
    private $loaded = false;
    private $mongo;
    private $args;

    public function __construct() {
        $this->args = func_get_args();
    }
    private function init() {
        if (!$this->loaded) {
            $t = create_function(null, 'new Mongo('. implode(',', $this->args) .');');
            $this->mongo = $t();
            $this->loaded = true;
        }
    }
    public function __call($name, array $arguments) {
        $this->init();
        return call_user_func_array(array($this->mongo, $name), $arguments);
    }
    public static function __callStatic($name, array $arguments) {
        $this->__call($name, $arguments);
    }
    public function __get($name) {
        $this->init();
        return $this->mongo->$name;
    }
    public function __set($name, $value) {
        $this->init();
        return $this->mongo->$name = $value;
    }
    public function __isset($name) {
        $this->init();
        return isset($this->mongo->$name);
    }
    public function __unset($name) {
        $this->init();
        unset($this->mongo->$name);
    }
}

And it remains to replace new Mongo with new Mongo_LazyLoad .
This method is more suitable for making a minimum of changes in an existing project.

C
casey, 2011-02-01
@casey

In the end, I came to this semi-solution:

  public static function getMongoDb ()
  {
    static $mongodb, $mongo;

    if ($mongodb === null) {
      $mongo = new \Mongo (MONGO_SERVER);
      $mongodb = $mongo->selectDB (MONGO_DATABASE);
    }

    return $mongodb;
  }

Yes, when you select a collection, a connection to mongo is still made, but if Mongo is not used anywhere on the page, objects for working with it will not be created.

V
Vladimir Chernyshev, 2011-02-01
@VolCh

Didn’t work tightly, I don’t know if there is exactly what you need, but Doctrine has a binding for Mongo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question