Skip to content
Newer
Older
100644 229 lines (208 sloc) 5.83 KB
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
1 <?php
df137f1 @canni Added File level doccblocks
authored Dec 16, 2010
2 /**
3 * EMongoDB.php
4 *
5 * PHP version 5.2+
6 *
7 * @author Dariusz Górecki <darek.krk@gmail.com>
99e5755 @canni Updated DocBlocks
authored Dec 24, 2010
8 * @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
1ca2373 @canni Prepare to release v1.3.5
authored Jan 7, 2011
9 * @copyright 2011 CleverIT http://www.cleverit.com.pl
df137f1 @canni Added File level doccblocks
authored Dec 16, 2010
10 * @license http://www.yiiframework.com/license/ BSD license
11 * @version 1.3
12 * @category ext
13 * @package ext.YiiMongoDbSuite
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
14 * @since v1.0
df137f1 @canni Added File level doccblocks
authored Dec 16, 2010
15 */
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
16
17 /**
18 * EMongoDB
19 *
20 * This is merge work of tyohan, Alexander Makarov and mine
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
21 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
22 */
23 class EMongoDB extends CApplicationComponent
24 {
25 /**
26 * @var string host:port
27 *
28 * Correct syntax is:
29 * mongodb://[username:password@]host1[:port1][,host2[:port2:],...]
30 *
31 * @example mongodb://localhost:27017
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
32 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
33 */
34 public $connectionString;
35
36 /**
37 * @var boolean $autoConnect whether the Mongo connection should be automatically established when
38 * the component is being initialized. Defaults to true. Note, this property is only
39 * effective when the EMongoDB object is used as an application component.
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
40 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
41 */
42 public $autoConnect = true;
43
44 /**
45 * @var false|string $persistentConnection false for non-persistent connection, string for persistent connection id to use
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
46 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
47 */
48 public $persistentConnection = false;
49
50 /**
51 * @var string $dbName name of the Mongo database to use
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
52 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
53 */
54 public $dbName = null;
55
56 /**
57 * @var MongoDB $_mongoDb instance of MongoDB driver
58 */
59 private $_mongoDb;
60
61 /**
62 * @var Mongo $_mongoConnection instance of MongoDB driver
63 */
64 private $_mongoConnection;
65
66 /**
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
67 * If set to TRUE all internal DB operations will use FSYNC flag with data modification requests,
68 * in other words, all write operations will have to wait for a disc sync!
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
69 *
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
70 * MongoDB default value for this flag is: FALSE.
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
71 *
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
72 * @var boolean $fsyncFlag state of FSYNC flag to use with internal connections (global scope)
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
73 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
74 */
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
75 public $fsyncFlag = false;
df137f1 @canni Added File level doccblocks
authored Dec 16, 2010
76
7e7dcc7 @josemartinez MongoDB GridFS Support implemented by:
josemartinez authored Dec 16, 2010
77 /**
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
78 * If set to TRUE all internal DB operations will use SAFE flag with data modification requests.
7e7dcc7 @josemartinez MongoDB GridFS Support implemented by:
josemartinez authored Dec 16, 2010
79 *
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
80 * When SAFE flag is set to TRUE driver will wait for the response from DB, and throw an exception
81 * if something went wrong, is fet to false, driver will only send operation to DB but will not wait
82 * for response from DB.
7e7dcc7 @josemartinez MongoDB GridFS Support implemented by:
josemartinez authored Dec 16, 2010
83 *
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
84 * MongoDB default value for this flag is: FALSE.
85 *
86 * @var boolean $safeFlag state of SAFE flag (global scope)
7e7dcc7 @josemartinez MongoDB GridFS Support implemented by:
josemartinez authored Dec 16, 2010
87 */
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
88 public $safeFlag = false;
df137f1 @canni Added File level doccblocks
authored Dec 16, 2010
89
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
90 /**
bc16d1e @canni Added support to set user-defined value for use cursor flag, analogic…
authored Dec 24, 2010
91 * If set to TRUE findAll* methods of models, will return {@see EMongoCursor} instead of
92 * raw array of models.
93 *
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
94 * Generally you should want to have this set to TRUE as cursor use lazy-loading/instaninating of
bc16d1e @canni Added support to set user-defined value for use cursor flag, analogic…
authored Dec 24, 2010
95 * models, this is set to FALSE, by default to keep backwards compatibility.
96 *
97 * Note: {@see EMongoCursor} does not implement ArrayAccess interface and cannot be used like an array,
98 * because offset access to cursor is highly ineffective and pointless.
99 *
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
100 * @var boolean $useCursor state of Use Cursor flag (global scope)
bc16d1e @canni Added support to set user-defined value for use cursor flag, analogic…
authored Dec 24, 2010
101 */
45aad51 @canni Change the default state of FSync and Safe flags to MongoDB default
authored Dec 26, 2010
102 public $useCursor = false;
bc16d1e @canni Added support to set user-defined value for use cursor flag, analogic…
authored Dec 24, 2010
103
104 /**
b70f415 @canni Convert GridFS temp folder into a scope flag
authored Dec 30, 2010
105 * Storage location for temporary files used by the GridFS Feature.
106 * If set to null, component will not use temporary storage
107 * @var string $gridFStemporaryFolder
108 */
109 public $gridFStemporaryFolder = null;
110
111 /**
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
112 * Connect to DB if connection is already connected this method doeas nothing
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
113 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
114 */
115 public function connect()
116 {
117 if(!$this->getConnection()->connected)
118 return $this->getConnection()->connect();
119 }
120
121 /**
122 * Returns Mongo connection instance if not exists will create new
123 *
124 * @return Mongo
125 * @throws EMongoException
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
126 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
127 */
128 public function getConnection()
129 {
130 if($this->_mongoConnection === null)
131 {
132 try
133 {
134 Yii::trace('Opening MongoDB connection', 'ext.MongoDb.EMongoDB');
135 if(empty($this->connectionString))
136 throw new EMongoException(Yii::t('yii', 'EMongoDB.connectionString cannot be empty.'));
137
6484d86 @canni Fix nasty bug about connecting to mongo
authored Nov 30, 2010
138 if($this->persistentConnection !== false)
139 $this->_mongoConnection = new Mongo($this->connectionString, array(
140 'connect'=>$this->autoConnect,
141 'persist'=>$this->persistentConnection
142 ));
143 else
144 $this->_mongoConnection = new Mongo($this->connectionString, array(
145 'connect'=>$this->autoConnect,
146 ));
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
147
148 return $this->_mongoConnection;
149 }
150 catch(MongoConnectionException $e)
151 {
152 throw new EMongoException(Yii::t(
153 'yii',
154 'EMongoDB failed to open connection: {error}',
155 array('{error}'=>$e->getMessage())
156 ), $e->getCode());
157 }
158 }
159 else
160 return $this->_mongoConnection;
161 }
162
163 /**
164 * Set the connection
165 *
166 * @param Mongo $connection
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
167 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
168 */
169 public function setConnection(Mongo $connection)
170 {
171 $this->_mongoConnection = $connection;
172 }
173
174 /**
175 * Get MongoDB instance
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
176 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
177 */
178 public function getDbInstance()
179 {
180 if($this->_mongoDb === null)
181 return $this->_mongoDb = $this->getConnection()->selectDB($this->dbName);
182 else
183 return $this->_mongoDb;
184 }
185
186 /**
187 * Set MongoDB instance
188 * Enter description here ...
189 * @param string $name
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
190 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
191 */
192 public function setDbInstance($name)
193 {
194 $this->_mongoDb = $this->getConnection()->selectDb($name);
195 }
196
197 /**
198 * Closes the currently active Mongo connection.
199 * It does nothing if the connection is already closed.
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
200 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
201 */
202 protected function close(){
203 if($this->_mongoConnection!==null){
204 $this->_mongoConnection->close();
205 $this->_mongoConnection=null;
206 Yii::trace('Closing MongoDB connection', 'ext.MongoDb.EMongoDB');
207 }
208 }
209
210 /**
211 * If we have don't use presist connection, close it
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
212 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
213 */
214 public function __destruct(){
215 if(!$this->persistentConnection){
216 $this->close();
217 }
218 }
219
220 /**
221 * Drop the current DB
dbb1b84 @canni Added Since v1.0
authored Jan 7, 2011
222 * @since v1.0
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
223 */
224 public function dropDb()
225 {
2accaf0 @canni Clean out the empty whitespace lines
authored Jan 23, 2011
226 $this->_mongoDb->drop();
2c46208 @canni First refactors to Use Alexander Makarov code
authored Nov 29, 2010
227 }
228 }
Something went wrong with that request. Please try again.