/** * Registers the module-only services * * @param Phalcon\DI $di */ public function registerServices($di) { /** * Read configuration */ $config = (include __DIR__ . "/config/config.php"); $di['view']->setViewsDir(__DIR__ . '/views/'); /** * The database connection is created based on the parameters defined in the configuration file */ $di['db'] = function () use($config) { return new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname)); }; /** * The mongo connection is created */ $di['mongo'] = function () { $mongo = new \Mongo(); return $mongo->selectDb("biko"); }; /** * ODM Collection Manager */ $di['collectionManager'] = function () { return new CollectionManager(); }; /** * Unique Cart ID generator */ $di['unique'] = function () { return new Generator(); }; }
protected static function createConnection() { $server = isset($GLOBALS['mongo_server']) ? $GLOBALS['mongo_server'] : 'mongodb://localhost:27017'; $dbName = isset($GLOBALS['mongo_db_name']) ? $GLOBALS['mongo_db_name'] : 'task_queue_tests'; $mongo = new \Mongo($server); return $mongo->selectDb($dbName); }
/** * {@inheritdoc} */ protected function _connect() { if (!$this->isConnected()) { $this->_connection = new \MongoClient($this->_hostString, $this->_config['options']); $this->_connection->connect(); $this->_db = $this->_connection->selectDb($this->_config['options']['db']); } }
public function saveDataToMongo($data) { $connectTo = "{$this->host}:{$this->port}"; $mongo = new Mongo($connectTo); $collection = $mongo->selectDb($this->database)->selectCollection($this->collection); foreach ($data as $k => $v) { if ($v) { $collection->insert((array) $v); } } }
public function auth($username, $password, $db = "admin") { if ($db === "") { if (!$this->_mongoAuth && $this->_mongoDb) { $db = $this->_mongoDb; } else { $db = "admin"; } } $server = $this->_mongoHost . ":" . $this->_mongoPort; if (!$this->_mongoPort) { $server = $this->_mongoHost; } try { $this->_mongo = new Mongo($server, $this->_mongoOptions); $this->_mongo->setSlaveOkay(true); } catch (Exception $e) { echo "Unable to connect MongoDB, please check your configurations. MongoDB said:" . $e->getMessage() . "."; exit; } // changing timeout to the new value MongoCursor::$timeout = $this->_mongoTimeout; //auth by mongo if ($this->_mongoAuth) { $dbs = $db; if (!is_array($dbs)) { $dbs = preg_split("/\\s*,\\s*/", $dbs); } foreach ($dbs as $db) { $ret = $this->_mongo->selectDb($db)->authenticate($username, $password); if (!$ret["ok"]) { return false; } } } else { if ($this->_controlAuth) { if (!isset($this->_controlUsers[$username]) || $this->_controlUsers[$username] != $password) { return false; } //authenticate if (!empty($this->_mongoUser)) { return $this->_mongo->selectDB($db)->authenticate($this->_mongoUser, $this->_mongoPass); } } else { //authenticate if (!empty($this->_mongoUser)) { return $this->_mongo->selectDB($db)->authenticate($this->_mongoUser, $this->_mongoPass); } } } return true; }
public function getDataFromMongo() { $connectTo = "{$this->from_host}:{$this->from_port}"; try { $mongo = new Mongo($connectTo); $collection = $mongo->selectDb($this->from_database)->selectCollection($this->from_collection); } catch (Exception $e) { die("Nie mogę nazwiązać połączenia z bazą mongo ({$connectTo} - db:{$this->from_database}, col: {$this->from_collection}) "); } // find everything in the collection $cursor = $collection->find(); $array_out = array(); foreach ($cursor as $obj) { $array_out[] = $obj; } return $array_out; }
/** * Registers the module-only services * * @param \Phalcon\DiInterface $di */ public function registerServices($di) { /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->set('moduleConfig', $moduleConfig); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); /** * Module specific dispatcher */ $di->set('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($di->getShared('eventsManager')); $dispatcher->setDefaultNamespace('App\\Modules\\Frontend\\'); return $dispatcher; }); $di->setShared('request', function () use($appConfig) { return new \Phalcon\Http\Request(); }); /** * Include config per environment */ include __DIR__ . '/config/config_' . $appConfig->application->environment . '.php'; $database = $di->getConfig()->application->site . $di->get('request')->getQuery("country_code"); /** * Simple database connection to localhost */ $di->setShared('mongo', function ($config, $database) { $mongo = new \Mongo(); return $mongo->selectDb($config->{$database}->dbname); }, true); $di->setShared('collectionManager', function () { return new \Phalcon\Mvc\Collection\Manager(); }, true); }
public function auth($username, $password, $db = "admin") { if ($db === "") { if (!$this->_mongoAuth && $this->_mongoDb) { $db = $this->_mongoDb; } else { $db = "admin"; } } $this->_mongo = new Mongo($this->_mongoHost . ":" . $this->_mongoPort); //auth by mongo if ($this->_mongoAuth) { $dbs = $db; if (!is_array($dbs)) { $dbs = preg_split("/\\s*,\\s*/", $dbs); } foreach ($dbs as $db) { $ret = $this->_mongo->selectDb($db)->authenticate($username, $password); if (!$ret["ok"]) { return false; } } } else { if ($this->_controlAuth) { if (!isset($this->_controlUsers[$username]) || $this->_controlUsers[$username] != $password) { return false; } //authenticate if (!empty($this->_mongoUser)) { return $this->_mongo->selectDB($db)->authenticate($this->_mongoUser, $this->_mongoPass); } } else { //authenticate if (!empty($this->_mongoUser)) { return $this->_mongo->selectDB($db)->authenticate($this->_mongoUser, $this->_mongoPass); } } } return true; }
<?php //Create and instance of Morph_Storage passing in the appropriate database $mongo = new Mongo(); $storage = new Morph_Storage($mongo->selectDb('myDB')); //Find a maximum of 10 users that has more than 15 posts, skipping the first 10 $query = new Morph_Query(); $query->limit(10)->skip(10)->property('numberOfPosts')->greaterThan(15); $users = $storage->findByQuery(new User(), $query);
<?php //Initialise Morph_Storage passing in the appropriate database $mongo = new Mongo(); Morph_Storage::init($mongo->selectDb('myDB'));; //Find users with the userName = j.d.moss $user = new User(); $query = new Morph_Query(); $query->property('userName')->equals('j.d.moss'); $users = $user->findByQuery($query);
function getDefaultConnectionPeer() { define('DB_NAME', 'links'); $m = new Mongo(); return new LinkPeer($m->selectDb(DB_NAME)); }
$di['assets'] = function () { $assets = new AssetsManager(); $assets->addJs('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', false)->addJs('//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js', false); return $assets; }; /** * Flash service with custom CSS classes */ $di['flash'] = function () { return new Flash(array('error' => 'alert alert-error', 'success' => 'alert alert-success', 'notice' => 'alert alert-info')); }; /** * Flash service with custom CSS classes */ $di['flashDirect'] = function () { return new FlashDirect(array('error' => 'alert alert-error', 'success' => 'alert alert-success', 'notice' => 'alert alert-info')); }; $di['session'] = function () { $session = new Session(array('uniqueId' => 'dasshy-')); $session->start(); return $session; }; // Simple database connection to localhost $di['mongo'] = function () { $mongo = new Mongo(); return $mongo->selectDb("stats"); }; //Collection manager $di['collectionManager'] = function () { return new CollectionManager(); };
public function testNestedArray() { $count = 1500; $mongo = new Mongo(); $mongoDB = $mongo->selectDb('testdb'); $mongoCollection = $mongoDB->selectCollection('testcollection'); $values = array(); for ($i = 0; $i < $count; $i++) { $values[] = new MongoId($i); } $mongoCursor = $mongoCollection->find(array('_id' => array('$in' => $values))); while ($mongoCursor->hasNext()) { $mongoCursor->getNext(); } }
<?php foreach ($dbs['databases'] as $db) { if ($db['name'] === 'local' || $db['name'] === 'admin') { continue; } ?> <tr> <td><a href="<?php echo $_SERVER['PHP_SELF'] . '?db=' . $db['name']; ?> "><?php echo $db['name']; ?> </a></td> <td><?php echo count($mongo->selectDb($db['name'])->listCollections()); ?> </td> <?php if ($readOnly !== true) { ?> <td><a href="<?php echo $_SERVER['PHP_SELF']; ?> ?delete_db=<?php echo $db['name']; ?> " onClick="return confirm('Are you sure you want to delete this database?');">Delete</a></td> <?php } else {
#!/usr/bin/env php <?php require_once dirname(__FILE__) . '/Link.php'; require_once dirname(__FILE__) . '/LinkPeer.php'; define('DB_NAME', 'links'); $m = new Mongo(); $linkPeer = new LinkPeer($m->selectDb(DB_NAME)); // Ensure the db is empty foreach ($linkPeer->fetchAll() as $link) { $linkPeer->delete($link); } // Print the contens of the database echo "> initial database values:\n"; foreach ($linkPeer->fetchByTag('com') as $link) { echo "{$link}\n"; } echo "\n----\n"; // Add some links $lnk1 = new Link(); $lnk1->setUrl('http://www.google.com'); $lnk1->addTag('google'); $lnk1->addTag('com'); $lnk2 = new Link(); $lnk2->setUrl('http://www.mongodb.org'); $lnk2->addTag('mongodb'); $lnk2->addTag('org'); $lnk3 = new Link(); $lnk3->setUrl('http://www.ibuildings.com'); $lnk3->addTag('ibuildings'); $lnk3->addTag('com'); $lnk4 = new Link();
<?php // This service returns a mongo database at 192.168.1.100 $di->set('mongo1', function () { $mongo = new Mongo("mongodb://*****:*****@192.168.1.100"); return $mongo->selectDb("management"); }, true); // This service returns a mongo database at localhost $di->set('mongo2', function () { $mongo = new Mongo("mongodb://localhost"); return $mongo->selectDb("invoicing"); }, true);
<?php // Simple database connection to localhost $di->set('mongo', function () { $mongo = new Mongo(); return $mongo->selectDb("store"); }, true); // Connecting to a domain socket, falling back to localhost connection $di->set('mongo', function () { $mongo = new Mongo("mongodb:///tmp/mongodb-27017.sock,localhost:27017"); return $mongo->selectDb("store"); }, true);
/** * Регистрация сервисов модуля */ public function registerServices($di) { $config = new \Phalcon\Config\Adapter\Ini(__DIR__ . '/config/config.ini'); // Set site config $di->set('config', $config); // Setting up mongo $di->set('mongo', function () use($config) { $mongo = new \Mongo($config->database->host); return $mongo->selectDb($config->database->name); }, true); // Registering the collectionManager service $di->set('collectionManager', function () { $modelsManager = new \Phalcon\Mvc\Collection\Manager(); return $modelsManager; }, true); // Start the session the first time when some component request the session service $di->set('session', function () use($config) { if (isset($config->session->adapter)) { switch ($config->session->adapter) { case 'mongo': $mongo = new \Mongo($config->session->mongoHost); $session = new \Phalcon\Session\Adapter\Mongo(array('collection' => $mongo->kladrapiSession->data)); break; case 'file': $session = new \Phalcon\Session\Adapter\Files(); break; } } else { $session = new \Phalcon\Session\Adapter\Files(); } $session->start(); return $session; }); // Setting up dispatcher $di->set('dispatcher', function () use($di) { $evManager = $di->getShared('eventsManager'); $evManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) { switch ($exception->getCode()) { case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('controller' => 'index', 'action' => 'show404')); return false; } }); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace("Kladr\\Frontend\\Controllers"); $dispatcher->setEventsManager($evManager); return $dispatcher; }); // Register an user component $di->set('elements', function () { return new Library\Elements(); }); // Register key tools $di->set('keyTools', function () { return new Plugins\KeyTools(); }); // Register the flash service with custom CSS classes $di->set('flash', function () { $flash = new \Phalcon\Flash\Direct(); return $flash; }); // Setting up the view component $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../apps/frontend/views/'); return $view; }); }
$dbConfig = $config->database->toArray(); $adapter = $dbConfig['adapter']; unset($dbConfig['adapter']); $class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter; return new $class($dbConfig); }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $di->setShared('modelsMetadata', function () { return new MetaDataAdapter(); }); /** * Start the session the first time some component request the session service */ $di->setShared('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); // Simple database connection to localhost $di->set('mongo', function () { $mongo = new Mongo(); return $mongo->selectDb("cek_ongkir"); }, true); $di->set('collectionManager', function () { return new Phalcon\Mvc\Collection\Manager(); }, true);
protected function initMongo() { $config = $this->di['config']; $this->di->set('mongo', function () use($config) { $mongo = new Mongo(); return $mongo->selectDb($config->mongodb->dbname); }, TRUE); }