Database.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\mongodb;
  8. use yii\base\BaseObject;
  9. use Yii;
  10. /**
  11. * Database represents the MongoDB database information.
  12. *
  13. * @property file\Collection $fileCollection Mongo GridFS collection. This property is read-only.
  14. *
  15. * @author Paul Klimov <klimov.paul@gmail.com>
  16. * @since 2.0
  17. */
  18. class Database extends BaseObject
  19. {
  20. /**
  21. * @var Connection MongoDB connection.
  22. */
  23. public $connection;
  24. /**
  25. * @var string name of this database.
  26. */
  27. public $name;
  28. /**
  29. * @var Collection[] list of collections.
  30. */
  31. private $_collections = [];
  32. /**
  33. * @var file\Collection[] list of GridFS collections.
  34. */
  35. private $_fileCollections = [];
  36. /**
  37. * Returns the Mongo collection with the given name.
  38. * @param string $name collection name
  39. * @param bool $refresh whether to reload the collection instance even if it is found in the cache.
  40. * @return Collection Mongo collection instance.
  41. */
  42. public function getCollection($name, $refresh = false)
  43. {
  44. if ($refresh || !array_key_exists($name, $this->_collections)) {
  45. $this->_collections[$name] = $this->selectCollection($name);
  46. }
  47. return $this->_collections[$name];
  48. }
  49. /**
  50. * Returns Mongo GridFS collection with given prefix.
  51. * @param string $prefix collection prefix.
  52. * @param bool $refresh whether to reload the collection instance even if it is found in the cache.
  53. * @return file\Collection Mongo GridFS collection.
  54. */
  55. public function getFileCollection($prefix = 'fs', $refresh = false)
  56. {
  57. if ($refresh || !array_key_exists($prefix, $this->_fileCollections)) {
  58. $this->_fileCollections[$prefix] = $this->selectFileCollection($prefix);
  59. }
  60. return $this->_fileCollections[$prefix];
  61. }
  62. /**
  63. * Selects collection with given name.
  64. * @param string $name collection name.
  65. * @return Collection collection instance.
  66. */
  67. protected function selectCollection($name)
  68. {
  69. return Yii::createObject([
  70. 'class' => 'yii\mongodb\Collection',
  71. 'database' => $this,
  72. 'name' => $name,
  73. ]);
  74. }
  75. /**
  76. * Selects GridFS collection with given prefix.
  77. * @param string $prefix file collection prefix.
  78. * @return file\Collection file collection instance.
  79. */
  80. protected function selectFileCollection($prefix)
  81. {
  82. return Yii::createObject([
  83. 'class' => 'yii\mongodb\file\Collection',
  84. 'database' => $this,
  85. 'prefix' => $prefix,
  86. ]);
  87. }
  88. /**
  89. * Creates MongoDB command associated with this database.
  90. * @param array $document command document contents.
  91. * @return Command command instance.
  92. * @since 2.1
  93. */
  94. public function createCommand($document = [])
  95. {
  96. return $this->connection->createCommand($document, $this->name);
  97. }
  98. /**
  99. * Creates new collection.
  100. * Note: Mongo creates new collections automatically on the first demand,
  101. * this method makes sense only for the migration script or for the case
  102. * you need to create collection with the specific options.
  103. * @param string $name name of the collection
  104. * @param array $options collection options in format: "name" => "value"
  105. * @return bool whether operation was successful.
  106. * @throws Exception on failure.
  107. */
  108. public function createCollection($name, $options = [])
  109. {
  110. return $this->createCommand()->createCollection($name, $options);
  111. }
  112. /**
  113. * Drops specified collection.
  114. * @param string $name name of the collection
  115. * @return bool whether operation was successful.
  116. * @since 2.1
  117. */
  118. public function dropCollection($name)
  119. {
  120. return $this->createCommand()->dropCollection($name);
  121. }
  122. /**
  123. * Returns the list of available collections in this database.
  124. * @param array $condition filter condition.
  125. * @param array $options options list.
  126. * @return array collections information.
  127. * @since 2.1.1
  128. */
  129. public function listCollections($condition = [], $options = [])
  130. {
  131. return $this->createCommand()->listCollections($condition, $options);
  132. }
  133. /**
  134. * Clears internal collection lists.
  135. * This method can be used to break cycle references between [[Database]] and [[Collection]] instances.
  136. */
  137. public function clearCollections()
  138. {
  139. $this->_collections = [];
  140. $this->_fileCollections = [];
  141. }
  142. }