本文整理汇总了PHP中Mongo::connect方法的典型用法代码示例。如果您正苦于以下问题:PHP Mongo::connect方法的具体用法?PHP Mongo::connect怎么用?PHP Mongo::connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mongo
的用法示例。
在下文中一共展示了Mongo::connect方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: connect
/**
* Connect to MongoDB, select database
*
* @return bool
*/
public function connect()
{
if ($this->_connection) {
return;
}
/**
* Add required variables
* Clear the connection parameters for security
*/
$options = $this->_config->assoc();
unset($this->_config);
$hostname = isAke($options, 'hostname', 'localhost');
$port = isAke($options, 'port', '27017');
$conn = 'mongodb://' . $hostname . ':' . $port;
unset($options['hostname']);
unset($options['port']);
$this->_connection = new \Mongo($conn, $options);
/* Try connect */
try {
$this->_connection->connect();
} catch (\MongoConnectionException $e) {
throw new \Exception('Unable to connect to MongoDB server at ' . $hostname);
}
$this->_db = new \MongoDB($this->_connection, $options['db']);
return $this->_connected = TRUE;
}
示例2: connect
/**
* Connect to the database.
*
* @return boolean connection result
*/
public function connect()
{
if ($this->connection->connect()) {
$this->connected = true;
return true;
}
return false;
}
示例3: connect
/**
* Connects to our database
*/
public function connect()
{
// We don't need to throw useless exceptions here, the MongoDB PHP Driver has its own checks and error reporting
// Yii will easily and effortlessly display the errors from the PHP driver, we should only catch its exceptions if
// we wanna add our own custom messages on top which we don't, the errors are quite self explanatory
if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
$this->_mongo = new Mongo($this->server, $this->options);
$this->_mongo->connect();
if ($this->setSlaveOkay) {
$this->_mongo->setSlaveOkay($this->setSlaveOkay);
}
} else {
$this->_mongo = new MongoClient($this->server, $this->options);
if (is_array($this->RP)) {
$const = $this->RP[0];
$opts = $this->RP[1];
if (!empty($opts)) {
// I do this due to a bug that exists in some PHP driver versions
$this->_mongo->setReadPreference(constant('MongoClient::' . $const), $opts);
} else {
$this->_mongo->setReadPreference(constant('MongoClient::' . $const));
}
}
}
}
示例4: insert_from_cursor
function insert_from_cursor()
{
$m = new Mongo("localhost");
$m->connect();
$db = $m->selectDB("test-database");
$collection = $db->successfulTweets;
$successfulTweets = $collection->find();
// echo "<pre>";
foreach ($successfulTweets as $tweet) {
// var_dump($tweet);
$text = $tweet["text"];
$author = $tweet["user_name"];
$tweet_url = "https://twitter.com/statuses/" . $tweet["tweet_id"];
$handle_url = "https://twitter.com/" . $tweet["user_name"];
if ($tweet["tweet_id"] == Null) {
$title = "<span>--manual_post<span>: " . $text;
$content = "This post was manually submitted";
$author = "--manual_post";
} else {
$title = "<span>" . $author . "</span>: " . $text . "<p> - [<a href=\"" . $tweet_url . "\" class=\"header-tweet-link\">link</a> | <a href=\"" . $handle_url . "\" class=\"header-handle-link\">@" . $author . "</a>]</p>";
$content = "<a href=\"" . $tweet_url . "\">Check out the original tweet here: https://twitter.com/statuses/" . $tweet["tweet_id"];
$author = $tweet["user_name"];
}
if ($tweet["tweeted_yet"] == True) {
// echo "Already posted";
} else {
// echo "Not yet posted";
$myPost = array('post_title' => $title, 'post_content' => $content, 'post_status' => 'publish', 'post_author' => $author);
// var_dump($myPost);
$collection->update(array('_id' => $tweet['_id']), array('$set' => array('tweeted_yet' => True)));
$newVar = wp_insert_post($myPost);
}
}
// echo "</pre>";
}
示例5: _connect
/**
* {@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']);
}
}
示例6: testDropDB
public function testDropDB()
{
$this->object->connect();
$c = $this->object->selectCollection("temp", "foo");
$c->insert(array('x' => 1));
$this->object->dropDB("temp");
$this->assertEquals($c->findOne(), NULL);
$db = $this->object->selectDB("temp");
$c->insert(array('x' => 1));
$this->object->dropDB($db);
$this->assertEquals($c->findOne(), NULL);
}
示例7: getMongoConnection
private static function getMongoConnection($key)
{
$cd=&self::$dbStruct[$key]['data'];
if (!class_exists("Mongo"))
throw new RuntimeException("Please install the Mongo PHP extension if you want to use MongoDB databases. (pear install mongo)");
$mongo=new Mongo($cd['host'], array("connect"=>false, "db"=>$cd['database']));
if (!$mongo->connect())
throw new RuntimeException("Failed connecting to MongoDB key ".$key);
return self::$dbStruct[$key]['connection']=$mongo->selectDB($cd['database']);
}
示例8: connect
/**
* Connect to MongoDB, select database
*
* @return bool
*/
public function connect()
{
if ($this->_connection) {
return;
}
/**
* Add required variables
* Clear the connection parameters for security
*/
$options = $this->_config;
unset($this->_config);
$hostname = array_shift($options);
$conn = 'mongodb://' . $hostname;
$this->_connection = new \Mongo($conn, $options);
/* Try connect */
try {
$this->_connection->connect();
} catch (MongoConnectionException $e) {
throw new \Exception('Unable to connect to MongoDB server at ' . $hostname);
}
$this->_db = new \MongoDB($this->_connection, $options['db']);
return $this->_connected = TRUE;
}
示例9: testDropDB
public function testDropDB()
{
$this->object->connect();
$c = $this->object->selectCollection("temp", "foo");
$result = $c->db->command(array("ismaster" => 1));
if (!$result['ismaster']) {
$this->markTestSkipped("can't test writes on slave");
return;
}
$c->insert(array('x' => 1));
$this->object->dropDB("temp");
$this->assertEquals($c->findOne(), NULL);
$db = $this->object->selectDB("temp");
$c->insert(array('x' => 1));
$this->object->dropDB($db);
$this->assertEquals($c->findOne(), NULL);
}
示例10: connect
/**
* Connect to MongoDB, select database
*
* @throws \Exception
* @return bool
*/
public function connect()
{
if ($this->_connection && $this->_connection->connect()) {
return true;
}
/**
* Add required variables
* Clear the connection parameters for security
*/
$config = $this->_config['connection'] + array('hostnames' => 'localhost:27017');
unset($this->_config['connection']);
/* Add Username & Password to server string */
if (isset($config['username']) && isset($config['password'])) {
$config['hostnames'] = $config['username'] . ':' . $config['password'] . '@' . $config['hostnames'] . '/' . $config['database'];
}
/* Add required 'mongodb://' prefix */
if (strpos($config['hostnames'], 'mongodb://') !== 0) {
$config['hostnames'] = 'mongodb://' . $config['hostnames'];
}
if (!isset($config['options']) || !is_array($config['options'])) {
$config['options'] = array();
}
/* Create connection object, attempt to connect */
$config['options']['connect'] = false;
$class = '\\MongoClient';
if (!class_exists($class)) {
$class = '\\Mongo';
}
$this->_connection = new $class($config['hostnames'], $config['options']);
/* Try connect */
try {
$this->_connection->connect();
} catch (\MongoConnectionException $e) {
throw new \Exception('Unable to connect to MongoDB server at ' . $config['hostnames']);
}
if (!isset($config['database'])) {
throw new \Exception('No database specified in MangoDB Config');
}
$this->_db = $this->_connection->selectDB($config['database']);
if (!$this->_db instanceof \MongoDB) {
throw new \Exception('Unable to connect to database :: $_db is ' . gettype($this->_db));
}
$this->_connected = true;
return true;
}
示例11: __construct
/**
* Private constructor to satisfy the singleton design pattern. You should
* be calling MongoSession::init() prior to starting sessions.
*/
private function __construct()
{
//set the configs
$this->setConfig(self::$config);
//set the cookie settings
session_set_cookie_params(0, $this->getConfig('cookie_path'), $this->getConfig('cookie_domain'), $this->getConfig('cookie_secure'), $this->getConfig('cookie_httponly'));
//set HTTP cache headers
session_cache_limiter($this->getConfig('cache'));
session_cache_expire($this->getConfig('cache_expiry'));
//we need to ensure that PHP knows about our explicit timeout
ini_set('session.gc_maxlifetime', $this->getConfig('lifetime'));
//Mongo/MongoClient( uri, options )
$mongo_options = array();
foreach ($this->getConfig('connection_opts') as $optname => $optvalue) {
$mongo_options[$optname] = $optvalue;
}
//Mongo() defunct, use MongoClient() if available
$mongo_class = class_exists('MongoClient') ? 'MongoClient' : 'Mongo';
$this->conn = new $mongo_class($this->getConfig('connection'), $mongo_options);
if ($mongo_class == 'MongoClient') {
//set write concern from config
$this->instConfig['write_options'] = array('w' => $this->getConfig('write_concern'), 'j' => $this->getConfig('write_journal'));
} else {
//defunct 'safe' write, use safe mode if w > 0
$this->instConfig['write_options'] = array('safe' => $this->getConfig('write_concern') > 0);
}
//make the connection explicit
$this->conn->connect();
//init some variables for use
$db = $this->getConfig('db');
$coll = $this->getConfig('collection');
$lock = $this->getConfig('lockcollection');
//connect to the db and collections
$this->db = $this->conn->{$db};
$this->sessions = $this->db->{$coll};
$this->locks = $this->db->{$lock};
//tell PHP to use this class as the handler
session_set_save_handler(array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'), array($this, 'destroy'), array($this, 'gc'));
}
示例12: testPoolConnect1
public function testPoolConnect1() {
$conn = new Mongo();
$pool1 = MongoPool::info();
$orig = null;
foreach ($pool1 as $host => $info) {
if (strpos($host, 'localhost:27017...') === 0) {
$orig = $info;
}
}
$conn->connect();
$conn->connect();
$conn->connect();
$pool2 = MongoPool::info();
$followup = null;
foreach ($pool2 as $host => $info) {
if (strpos($host, 'localhost:27017...') === 0) {
$followup = $info;
}
}
$this->assertEquals($orig['in use'], $followup['in use']);
}
示例13: connect
/**
* Force the connection to be established.
* This will automatically be called by any MongoDB methods that are proxied via __call
*
* @return boolean
* @throws MongoException
*/
public function connect()
{
if (!$this->_connected) {
if ($this->profiling) {
$_bm = $this->profiler_start("Mongo_Database::{$this->_name}", "connect()");
}
$this->_connected = $this->_connection->connect();
if (isset($_bm)) {
$this->profiler_stop($_bm);
}
$this->_db = $this->_connection->selectDB("{$this->_db}");
}
return $this->_connected;
}
示例14: _connect
/**
* {@inheritdoc}
*/
protected function _connect()
{
if ($this->_options["connect"] == false) {
$this->_connection->connect();
}
}