PHP — how to connect to Mongo on demand (delay)?

0 like 0 dislike
3 views
Module to operate the Mongo part of the complex delivery PHP provides the ability to connect to the server immediately — allowing you to choose database and collection with no connection. To do this, there is such a design:

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

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

In this connection the question arose — maybe there is a ready solution, a layer to work with Mongo like Zend_Db or Rediska, where it is already implemented?

To connect each time it is pointless to reinvent the wheel — also I do not want, appealed to the collective mind :-)
by | 3 views

6 Answers

0 like 0 dislike
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); } } 
by
0 like 0 dislike
Under Zend is like except the backend for cache there is nothing.
by
0 like 0 dislike
Do The Lazy Load.
\r
The General pattern is as follows
\r
function makeRequest() { if (!$this->isConnected()) { $this->connect(); } parent::makeQuery(); } 

\r
And you will be a pending connection. I work with MySQL. Implementation details on your conscience :)
by
0 like 0 dislike
It's a little rewrite:
\r
<?php\rclass Mongo_LazyLoad {\rprivate $loaded = false;\rprivate $mongo;\rprivate $args;\r\rpublic function __construct() {\r$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); } } 

\r
Remains to replace new Mongo on new Mongo_LazyLoad.
This method is more suited to an already existing project to make a minimum of changes
by
0 like 0 dislike
In the end, I came to this floor solution:
\r
\r
 public static function getMongoDb () { static $mongodb $mongo; if ($mongodb === null) { $mongo = new \\Mongo (MONGO_SERVER); $mongodb = $mongo->selectDB (MONGO_DATABASE); } return $mongodb; } 

\r
Yes, the choice of the collection still runs the connection to mongo, but if anywhere on the page Mongo is not being used — object to work with it and will not be created.
by
0 like 0 dislike
Densely did not work, I do not know whether exactly what you need, but tying for Mongo have Doctrine
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
asked Mar 26, 2019 by anonimizer_me
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
29,061 users