本文整理汇总了PHP中MongoClient::selectDb方法的典型用法代码示例。如果您正苦于以下问题:PHP MongoClient::selectDb方法的具体用法?PHP MongoClient::selectDb怎么用?PHP MongoClient::selectDb使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoClient
的用法示例。
在下文中一共展示了MongoClient::selectDb方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: collection
/**
* Retrieves a collection object by name.
*
* @param string $name
* @return \MongoCollection
*/
public function collection($name)
{
if (empty($this->collections[$name])) {
$this->collections[$name] = new \MongoCollection($this->connection->selectDb($this->dbName), $name);
}
return $this->collections[$name];
}
示例2: register
/**
* {@inheritdoc}
* @see http://docs.phalconphp.com/pl/latest/reference/odm.html
*/
public function register(DiInterface $di)
{
$di->set(self::SERVICE_NAME, function () use($di) {
$mongoConfig = $di->get('config')->mongo->toArray();
if (isset($mongoConfig['dsn'])) {
$hostname = $mongoConfig['dsn'];
unset($mongoConfig['dsn']);
} else {
//obtains hostname
if (isset($mongoConfig['host'])) {
$hostname = 'mongodb://' . $mongoConfig['host'];
} else {
$hostname = 'mongodb://localhost';
}
if (isset($mongoConfig['port'])) {
$hostname .= ':' . $mongoConfig['port'];
}
//removes options that are not allowed in MongoClient constructor
unset($mongoConfig['host']);
unset($mongoConfig['port']);
}
$dbName = $mongoConfig['dbname'];
unset($mongoConfig['dbname']);
$mongo = new \MongoClient($hostname, $mongoConfig);
return $mongo->selectDb($dbName);
}, true);
}
示例3: register
/**
* {@inheritdoc}
*/
public function register(DiInterface $di)
{
$di->set(self::SERVICE_NAME, function () use($di) {
$mongo = new \MongoClient();
return $mongo->selectDb($di->get('config')->mongo->db);
}, true);
}
示例4: getMongo
public function getMongo($dsn, $options, $db)
{
$key = $dsn . "/" . $db;
if ($this->mongos[$key] == null) {
$mongoClient = new \MongoClient($dsn, $options);
$this->mongos[$key] = $mongoClient->selectDb($db);
}
return $this->mongos[$key];
}
示例5: setMongoDb
public function setMongoDb($dbName)
{
$this->set('mongo', function () {
$mongo = new \MongoClient("mongodb://localhost");
return $mongo->selectDb($dbName);
}, true);
$this->set('collectionManager', function () {
return new \Phalcon\Mvc\Collection\Manager();
}, true);
}
示例6: resolve
public function resolve(\Phalcon\Config $config)
{
$di = new \Phalcon\Di\FactoryDefault();
$di->set('config', $config, true);
$di->set('mongo', function () use($config) {
$connectionString = "mongodb://{$config->mongo->host}:{$config->mongo->port}";
$mongo = new \MongoClient($connectionString);
return $mongo->selectDb($config->mongo->dbname);
}, true);
$di->set('collectionManager', function () {
return new \Phalcon\Mvc\Collection\Manager();
}, true);
\Phalcon\Di::setDefault($di);
}
示例7: FlashSession
'error' => 'alert alert-error',
'success' => 'alert alert-success',
'notice' => 'alert alert-notice',
'warning' => 'alert alert-warning'
));
*/
/*
return new FlashSession(array(
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
'warning' => 'alert alert-warning'
));
*/
});
/**
* Register a user component
*/
$di->set('elements', function () {
return new Elements();
});
/**
* MongoDB
*/
$di->set('mongo', function () {
$mongo = new MongoClient();
return $mongo->selectDb("files");
}, true);
$di->set('collectionManager', function () {
return new Phalcon\Mvc\Collection\Manager();
}, true);
示例8: __construct
/**
* Const'r
*
* @param array $params Must contain:
* - connection: (Horde_Mongo_Client The Horde_Mongo instance.
*
* @return Horde_ActiveSync_State_Sql
*/
public function __construct(array $params = array())
{
parent::__construct($params);
if (empty($this->_params['connection']) || !$this->_params['connection'] instanceof MongoClient) {
throw new InvalidArgumentException('Missing or invalid connection parameter.');
}
$this->_mongo = $params['connection'];
$this->_db = $this->_mongo->selectDb(null);
}
示例9: function
return $session;
});
$di->set(MEMCACHE, function () {
$frontCache = new \Phalcon\Cache\Frontend\Data(array("lifetime" => 86400));
$cache = new \Phalcon\Cache\Backend\Memcache($frontCache, array("host" => "localhost", "port" => "11211", "prefix" => MEMCACHE));
return $cache;
});
$config = (require __DIR__ . "/../apps/common/configs/config.php");
$di->set(DATABASE, function () use($config) {
return new Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->dbs->host, "username" => $config->dbs->username, "password" => $config->dbs->password, "dbname" => $config->dbs->name, 'charset' => $config->dbs->charset));
});
$di->set('collectionManager', function () {
return new Phalcon\Mvc\Collection\Manager();
}, true);
$di->set('dbm', function () use($config) {
if (!$config->dbm->username or !$config->dbm->password) {
$mongo = new MongoClient('mongodb://' . $config->dbm->host);
} else {
$mongo = new MongoClient("mongodb://" . $config->dbm->username . ":" . $config->dbm->password . "@" . $config->dbm->host, array("db" => $config->dbm->name));
}
return $mongo->selectDb($config->dbm->name);
}, TRUE);
$application = new \Phalcon\Mvc\Application();
$application->setDI($di);
$application->registerModules(array('frontend' => array('className' => 'Modules\\Frontend\\Module', 'path' => '../apps/frontend/Module.php'), 'backend' => array('className' => 'Modules\\Backend\\Module', 'path' => '../apps/backend/Module.php')));
echo $application->handle()->getContent();
} catch (Phalcon\Exception $e) {
echo $e->getMessage();
} catch (PDOException $e) {
echo $e->getMessage();
}
示例10: foreach
<?php
foreach ($dbs['databases'] as $db) {
if ($db['name'] === 'local' || $db['name'] === 'admin') {
continue;
}
?>
<tr>
<td><a href="<?php
echo $_SERVER['PHP_SELF'] . '?db=' . urlencode($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 urlencode($db['name']);
?>
" onClick="return confirm('Are you sure you want to delete this database?');">Delete</a></td>
<?php
} else {
示例11: __construct
//.........这里部分代码省略.........
if (isset($options['cache_methods']['memcache']['object']) && $options['cache_methods']['memcache']['object']) {
$this->_memcache = $options['cache_methods']['memcache']['object'];
unset($options['cache_methods']['memcache']['object']);
} else {
if (isset($options['cache_methods']['memcache']['servers']) && $options['cache_methods']['memcache']['servers'] && !empty($options['cache_methods']['memcache']['servers'])) {
$memcacheType = false;
if (class_exists('\\Memcached')) {
$memcacheType = 'memcached';
} else {
if (class_exists('\\Memcache')) {
$memcacheType = 'memcache';
}
}
if (false !== $memcacheType) {
if ('memcached' === $memcacheType) {
$this->_memcache = new \Memcached();
} else {
if ('memcache' === $memcacheType) {
$this->_memcache = new \Memcache();
}
}
if ($this->_memcache) {
foreach ($options['cache_methods']['memcache']['servers'] as $val1) {
if ($val1) {
$opt1 = array_merge(array('host' => '127.0.0.1', 'port' => '11211', 'persistent' => true, 'weight' => 1), $val1);
$addServerStatus = false;
if ('memcached' === $memcacheType) {
$addServerStatus = $this->_memcache->addServer($opt1['host'], $opt1['port'], $opt1['weight']);
} else {
//memcache
$addServerStatus = $this->_memcache->addServer($opt1['host'], $opt1['port'], $opt1['persistent'], $opt1['weight']);
}
}
}
}
$memcacheVersion = false;
if ($this->_memcache) {
$memcacheVersion = $this->_memcache->getVersion();
}
if (false === $memcacheVersion) {
$this->_memcache = false;
} else {
$this->_memcacheType = $memcacheType;
}
unset($options['cache_methods']['memcache']['servers']);
}
}
}
}
/*
* Mongo
*/
if (isset($options['cache_methods']['mongo'])) {
if (isset($options['cache_methods']['mongo']['object']) && $options['cache_methods']['mongo']['object']) {
$this->_mongo = $options['cache_methods']['mongo']['object'];
unset($options['cache_methods']['mongo']['object']);
} else {
if (isset($options['cache_methods']['mongo']['servers']) && $options['cache_methods']['mongo']['servers'] && !empty($options['cache_methods']['mongo']['servers'])) {
if (isset($options['cache_methods']['mongo']['servers']['host']) && $options['cache_methods']['mongo']['servers']['host'] && isset($options['cache_methods']['mongo']['servers']['db']) && $options['cache_methods']['mongo']['servers']['db'] && isset($options['cache_methods']['mongo']['servers']['collection']) && $options['cache_methods']['mongo']['servers']['collection']) {
$mongoClient = new \MongoClient($options['cache_methods']['mongo']['servers']['host']);
if ($mongoClient) {
$this->_mongo = $mongoClient->selectDb($options['cache_methods']['mongo']['servers']['db'])->selectCollection($options['cache_methods']['mongo']['servers']['collection']);
}
}
unset($options['cache_methods']['mongo']['servers']);
}
}
}
/*
* File
*/
if (isset($options['cache_methods']['file'])) {
$isCacheMethodFileValid = false;
if (isset($options['cache_methods']['file']['cache_dir'])) {
if ($options['cache_methods']['file']['cache_dir']) {
if (!file_exists($options['cache_methods']['file']['cache_dir']) || !is_dir($options['cache_methods']['file']['cache_dir'])) {
@mkdir($options['cache_methods']['file']['cache_dir'], 0755, true);
}
if (file_exists($options['cache_methods']['file']['cache_dir']) && is_dir($options['cache_methods']['file']['cache_dir'])) {
if (is_readable($options['cache_methods']['file']['cache_dir']) && is_writable($options['cache_methods']['file']['cache_dir'])) {
$isCacheMethodFileValid = true;
}
}
}
}
if (!$isCacheMethodFileValid) {
unset($options['cache_methods']['file']);
}
}
if ($options['cache_methods'] && !empty($options['cache_methods'])) {
$this->_options = $options;
}
}
$options = null;
unset($options);
if (function_exists('igbinary_serialize')) {
$this->_has_igbinary = true;
}
$this->_key_salt = '_' . substr(hash('crc32b', $this->_serialize($this->_options)), 0, 2) . '_';
}
示例12: Application
// load classes
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Silex\Application;
// initialize Silex application
$app = new Application();
// load configuration from file
$app->config = $config;
// register Twig template provider
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/views'));
// register URL generator
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
// configure MongoDB client
$dbn = substr(parse_url($app->config['db_uri'], PHP_URL_PATH), 1);
$mongo = new MongoClient($app->config['db_uri'], array("connectTimeoutMS" => 30000));
$db = $mongo->selectDb($dbn);
// if BlueMix VCAP_SERVICES environment available
// overwrite with credentials from BlueMix
if ($services = getenv("VCAP_SERVICES")) {
$services_json = json_decode($services, true);
$app->config['weather_uri'] = $services_json["weatherinsights"][0]["credentials"]["url"];
}
// index page handlers
$app->get('/', function () use($app) {
return $app->redirect($app["url_generator"]->generate('index'));
});
$app->get('/index', function () use($app, $db) {
// get list of locations from database
// for each location, get current weather from Weather service
$collection = $db->locations;
$locations = iterator_to_array($collection->find());
示例13: function
$di->set('crm', function () use($config) {
return new Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->crm->host, "username" => $config->crm->username, "password" => $config->crm->password, "dbname" => $config->crm->name, 'charset' => $config->crm->charset));
});
$di->set('ads', function () use($config) {
return new Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->ads->host, "username" => $config->ads->username, "password" => $config->ads->password, "dbname" => $config->ads->name));
});
$di->set('collectionManager', function () {
return new Phalcon\Mvc\Collection\Manager();
}, true);
$di->set('bigdata', function () use($config) {
if (!$config->bigdata->username or !$config->bigdata->password) {
$mongo = new MongoClient('mongodb://' . $config->bigdata->host);
} else {
$mongo = new MongoClient("mongodb://" . $config->bigdata->username . ":" . $config->bigdata->password . "@" . $config->bigdata->host, array("db" => $config->bigdata->name));
}
return $mongo->selectDb($config->bigdata->name);
}, TRUE);
$di->set('crmcache', function () {
// Cache data for one day by default
$frontCache = new \Phalcon\Cache\Frontend\Data(array("lifetime" => 86400));
// Memcached connection settings
$cache = new \Phalcon\Cache\Backend\Memcache($frontCache, array("host" => "221.133.7.87", "port" => "11211", "prefix" => PREFIX_CACHE));
return $cache;
});
$di->set('crmcacheap', function () {
// Cache data for one day by default
$frontCache = new \Phalcon\Cache\Frontend\Data(array("lifetime" => 86400));
// Memcached connection settings
$cache = new \Phalcon\Cache\Backend\Memcache($frontCache, array("host" => "221.133.7.87", "port" => "11211", "prefix" => 'crmcacheap'));
return $cache;
});
示例14: json_encode
<?php
/* Standard options */
$uri = "mongodb://husseinsharif:Huss31n@ds041581.mongolab.com:41581/echo";
$options = array("connectTimeoutMS" => 30000);
/* Create Mongo connection with URI and options*/
$client = new MongoClient($uri, $options);
/* Reference the proper database */
$db = $client->selectDb("echo");
/* Reference the proper collection */
$reports = $db->selectCollection("reports");
// Find all markers
$cursor = $reports->find();
$allMarkers = array();
foreach ($cursor as $value) {
array_push($allMarkers, $value);
}
// Close DB
$client->close();
// Send stringified json back to Javascript
// header('Content-Type: application/json');
echo json_encode($allMarkers);
示例15: function
*/
$di->set('modelsMetadata', function () {
return new MetaData();
});
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
/**
* Register the flash service with custom CSS classes
*/
$di->set('flash', function () {
return new FlashSession(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info'));
});
/**
* Register a user component
*/
$di->set('elements', function () {
return new Elements();
});
$di->set('mongo', function () {
$mongo = new MongoClient();
return $mongo->selectDb("pidors");
}, true);
$di->set('collectionManager', function () {
return new Phalcon\Mvc\Collection\Manager();
}, true);