This repository was archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathMongoDbConnection.php
More file actions
76 lines (68 loc) · 1.6 KB
/
MongoDbConnection.php
File metadata and controls
76 lines (68 loc) · 1.6 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* This is a MongoDbConnection component that used for connect to mongoDb database
*
* @author yohan
*/
class MongoDbConnection extends CApplicationComponent
{
private $_dbConnection;
private $_db;
private $_user;
private $_password;
private $_host='localhost';
public function init()
{
parent::init();
}
protected function getConnection()
{
if($this->_dbConnection===NULL)
{
try
{
$this->_dbConnection= new Mongo($this->_host);
if($this->_user!==NULL && $this->_password!==NULL)
$this->_dbConnection->$db->authenticate($this->_user, $this->_password);
}
catch(MongoConnectionException $e)
{
throw new CDbException("Can't connect to Mongo DB");
}
}
return $this->_dbConnection;
}
protected function setDb($config)
{
$this->_db=$config;
}
protected function getDb()
{
$db=$this->_db;
return $this->connection->$db;
}
protected function setUser($config)
{
$this->_user=$config;
}
protected function getUser()
{
return $this->_user;
}
protected function setPassword($config)
{
$this->_password=$config;
}
protected function getPassword()
{
return $this->_password;
}
protected function setHost($config)
{
$this->_host=$config;
}
protected function getHost()
{
return $this->_host;
}
}