lithium\data\source\MongoDb::connect()
Implements
lithium\data\Source::connect()
Connects to the Mongo server. Matches up parameters from the constructor to create a Mongo database connection.
Returns
booleanReturns true
the connection attempt was successful, otherwise false
.
Links
Source
public function connect() {
$server = $this->_classes['server'];
if ($this->server && $this->server->getConnections() && $this->connection) {
return $this->_isConnected = true;
}
$this->_isConnected = false;
$options = [
'connect' => true,
'connectTimeoutMS' => $this->_config['timeout'],
'replicaSet' => $this->_config['replicaSet'],
];
try {
$this->server = new $server($this->_config['dsn'], $options);
if ($prefs = $this->_config['readPreference']) {
$prefs = !is_array($prefs) ? [$prefs, []] : $prefs;
$this->server->setReadPreference($prefs[0], $prefs[1]);
}
if ($this->connection = $this->server->{$this->_config['database']}) {
$this->_isConnected = true;
}
} catch (Exception $e) {
throw new NetworkException("Could not connect to the database.", 503, $e);
}
return $this->_isConnected;
}