ActiveFixture.php 4.04 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 34 35 36 37 38 39 40
    /**
     * @var Connection|string the DB connection object or the application component ID of the DB connection.
     */
    public $db = 'mongodb';
    /**
     * @var string|array the collection name that this fixture is about. If this property is not set,
     * the table name will be determined via [[modelClass]].
     * @see Connection::getCollection()
     */
    public $collectionName;
41

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

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

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

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

79 80 81 82
    protected function getCollection()
    {
        return $this->db->getCollection($this->getCollectionName());
    }
83

84 85 86 87 88 89 90
    protected function getCollectionName()
    {
        if ($this->collectionName) {
            return $this->collectionName;
        } else {
            /** @var ActiveRecord $modelClass */
            $modelClass = $this->modelClass;
91

92 93 94
            return $modelClass::collectionName();
        }
    }
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    /**
     * 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()
    {
        if ($this->dataFile === null) {
            $class = new \ReflectionClass($this);
            $dataFile = dirname($class->getFileName()) . '/data/' . $this->getCollectionName() . '.php';

            return is_file($dataFile) ? require($dataFile) : [];
        } else {
            return parent::getData();
        }
    }

    /**
     * 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
128
}