ActiveFixture.php 3.67 KB
Newer Older
1
<?php
Qiang Xue committed
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
7 8 9 10 11

namespace yii\mongodb;

use Yii;
use yii\base\InvalidConfigException;
Qiang Xue committed
12
use yii\test\BaseActiveFixture;
13

Qiang Xue committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 * ActiveFixture represents a fixture backed up by a [[modelClass|MongoDB ActiveRecord class]] or a [[collectionName|MongoDB collection]].
 *
 * Either [[modelClass]] or [[collectionName]] must be set. You should also provide fixture data in the file
 * specified by [[dataFile]] or overriding [[getData()]] if you want to use code to generate the fixture data.
 *
 * When the fixture is being loaded, it will first call [[resetCollection()]] to remove any existing data in the collection.
 * It will then populate the table with the data returned by [[getData()]].
 *
 * After the fixture is loaded, you can access the loaded data via the [[data]] property. If you set [[modelClass]],
 * you will also be able to retrieve an instance of [[modelClass]] with the populated data via [[getModel()]].
 *
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
class ActiveFixture extends BaseActiveFixture
30
{
31 32 33
	/**
	 * @var Connection|string the DB connection object or the application component ID of the DB connection.
	 */
34 35
	public $db = 'mongodb';
	/**
36
	 * @var string|array the collection name that this fixture is about. If this property is not set,
37
	 * the table name will be determined via [[modelClass]].
Carsten Brandt committed
38
	 * @see Connection::getCollection()
39 40 41
	 */
	public $collectionName;

42
	
43 44 45 46 47 48 49 50 51 52 53 54 55
	/**
	 * @inheritdoc
	 */
	public function init()
	{
		parent::init();
		if (!isset($this->modelClass) && !isset($this->collectionName)) {
			throw new InvalidConfigException('Either "modelClass" or "collectionName" must be set.');
		}
	}

	/**
	 * Loads the fixture data.
Mark committed
56
	 * Data will be batch inserted into the given collection.
57
	 */
58
	public function load()
59
	{
Mark committed
60 61
		parent::load();

62 63 64
		$data = $this->getData();
		$this->getCollection()->batchInsert($data);
		foreach ($data as $alias => $row) {
65 66 67 68
			$this->data[$alias] = $row;
		}
	}

Mark committed
69 70 71 72 73 74 75 76 77 78 79
	/**
	 * Unloads the fixture.
	 * 
	 * The default implementation will clean up the colection by calling [[resetCollection()]].
	 */
	public function unload()
	{
		$this->resetCollection();
		parent::unload();
	}

80 81 82 83 84 85 86 87 88 89
	protected function getCollection()
	{
		return $this->db->getCollection($this->getCollectionName());
	}

	protected function getCollectionName()
	{
		if ($this->collectionName) {
			return $this->collectionName;
		} else {
Qiang Xue committed
90
			/** @var ActiveRecord $modelClass */
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
			$modelClass = $this->modelClass;
			return $modelClass::collectionName();
		}
	}

	/**
	 * Returns the fixture data.
	 *
	 * This method is called by [[loadData()]] to get the needed fixture data.
	 *
	 * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
	 * The file should return an array of data rows (column name => column value), each corresponding to a row in the table.
	 *
	 * If the data file does not exist, an empty array will be returned.
	 *
	 * @return array the data rows to be inserted into the collection.
	 */
	protected function getData()
	{
Qiang Xue committed
110
		if ($this->dataFile === null) {
111 112
			$class = new \ReflectionClass($this);
			$dataFile = dirname($class->getFileName()) . '/data/' . $this->getCollectionName() . '.php';
Qiang Xue committed
113 114 115
			return is_file($dataFile) ? require($dataFile) : [];
		} else {
			return parent::getData();
116 117 118 119 120 121 122 123 124 125 126
		}
	}

	/**
	 * Removes all existing data from the specified collection and resets sequence number if any.
	 * This method is called before populating fixture data into the collection associated with this fixture.
	 */
	protected function resetCollection()
	{
		$this->getCollection()->remove();
	}
Qiang Xue committed
127
}