EMongoDB.php 5.8 KB

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