-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMongoConnection.php
More file actions
47 lines (39 loc) · 849 Bytes
/
MongoConnection.php
File metadata and controls
47 lines (39 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/**
* User: kpeeters
* Date: 24-7-13
* Time: 15:13
*/
namespace Qwad\Bundle\MongoDBBundle;
class MongoConnection
{
private $host;
private $port;
private $database;
private $connection;
public function __construct($host,$port,$database)
{
$this->host = $host;
$this->port = $port;
$this->database = $database;
}
public function getConnection()
{
if ( !$this->connection )
$this->connect();
return $this->connection;
}
private function connect()
{
$connectionString = 'mongodb://'.$this->host;
if ( $this->port )
$connectionString .= ':'.$this->port;
$connection = new \MongoClient($connectionString);
if ( $connection )
{
$this->connection = $connection->{$this->database};
return true;
}
return false;
}
}