Answer the question
In order to leave comments, you need to log in
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
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);
}
}
Under Zend, there seems to be nothing except for the backend for the cache.
Do a Lazy Load.
The general pattern is
function makeRequest() { if (!$this->isConnected()) { $this->connect(); } parent::makeQuery(); }
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);
}
}
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;
}
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 questionAsk a Question
731 491 924 answers to any question