Database.php 3.1 KB
Newer Older
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
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\mongo;

use yii\base\Object;
use Yii;

/**
 * Database represents the Mongo database information.
 *
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
class Database extends Object
{
	/**
	 * @var \MongoDB Mongo database instance.
	 */
	public $mongoDb;
	/**
	 * @var Collection[] list of collections.
	 */
	private $_collections = [];
29 30 31 32
	/**
	 * @var file\Collection[] list of GridFS collections.
	 */
	private $_fileCollections = [];
33 34 35 36

	/**
	 * Returns the Mongo collection with the given name.
	 * @param string $name collection name
37
	 * @param boolean $refresh whether to reload the collection instance even if it is found in the cache.
38 39 40 41 42 43 44 45 46 47
	 * @return Collection mongo collection instance.
	 */
	public function getCollection($name, $refresh = false)
	{
		if ($refresh || !array_key_exists($name, $this->_collections)) {
			$this->_collections[$name] = $this->selectCollection($name);
		}
		return $this->_collections[$name];
	}

48 49 50
	/**
	 * Returns Mongo GridFS collection with given prefix.
	 * @param string $prefix collection prefix.
51
	 * @param boolean $refresh whether to reload the collection instance even if it is found in the cache.
52 53 54 55 56 57 58 59 60 61
	 * @return file\Collection mongo GridFS collection.
	 */
	public function getFileCollection($prefix = 'fs', $refresh = false)
	{
		if ($refresh || !array_key_exists($prefix, $this->_fileCollections)) {
			$this->_fileCollections[$prefix] = $this->selectFileCollection($prefix);
		}
		return $this->_fileCollections[$prefix];
	}

62 63 64 65 66 67 68 69 70 71 72 73 74
	/**
	 * Selects collection with given name.
	 * @param string $name collection name.
	 * @return Collection collection instance.
	 */
	protected function selectCollection($name)
	{
		return Yii::createObject([
			'class' => 'yii\mongo\Collection',
			'mongoCollection' => $this->mongoDb->selectCollection($name)
		]);
	}

75 76 77 78 79 80 81 82 83 84 85 86 87
	/**
	 * Selects GridFS collection with given prefix.
	 * @param string $prefix file collection prefix.
	 * @return file\Collection file collection instance.
	 */
	protected function selectFileCollection($prefix)
	{
		return Yii::createObject([
			'class' => 'yii\mongo\file\Collection',
			'mongoCollection' => $this->mongoDb->getGridFS($prefix)
		]);
	}

88 89 90 91 92 93 94 95 96 97 98 99 100
	/**
	 * Creates new collection.
	 * Note: Mongo creates new collections automatically on the first demand,
	 * this method makes sense only for the migration script or for the case
	 * you need to create collection with the specific options.
	 * @param string $name name of the collection
	 * @param array $options collection options in format: "name" => "value"
	 * @return \MongoCollection new mongo collection instance.
	 */
	public function createCollection($name, $options = [])
	{
		return $this->mongoDb->createCollection($name, $options);
	}
101 102 103 104 105 106 107 108 109 110 111

	/**
	 * Executes Mongo command.
	 * @param array $command command specification.
	 * @param array $options options in format: "name" => "value"
	 * @return array database response.
	 */
	public function execute($command, $options = [])
	{
		return $this->mongoDb->command($command, $options);
	}
112
}