Class yii\mongodb\ActiveFixture

Inheritanceyii\mongodb\ActiveFixture » yii\test\BaseActiveFixture
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-mongodb/blob/master/src/ActiveFixture.php

ActiveFixture represents a fixture backed up by a modelClass or a 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 collection 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().

Public Properties

Hide inherited properties

Property Type Description Defined By
$collectionName string|array The collection name that this fixture is about. yii\mongodb\ActiveFixture
$db yii\mongodb\Connection|string The DB connection object or the application component ID of the DB connection. yii\mongodb\ActiveFixture

Public Methods

Hide inherited methods

Method Description Defined By
init() yii\mongodb\ActiveFixture
load() Loads the fixture data. yii\mongodb\ActiveFixture

Protected Methods

Hide inherited methods

Method Description Defined By
getCollection() Returns collection used by this fixture. yii\mongodb\ActiveFixture
getCollectionName() Returns collection name used by this fixture. yii\mongodb\ActiveFixture
getData() Returns the fixture data. yii\mongodb\ActiveFixture
resetCollection() Removes all existing data from the specified collection and resets sequence number if any. yii\mongodb\ActiveFixture

Property Details

Hide inherited properties

$collectionName public property

The collection name that this fixture is about. If this property is not set, the collection name will be determined via modelClass.

See also yii\mongodb\Connection::getCollection().

$db public property

The DB connection object or the application component ID of the DB connection.

public yii\mongodb\Connection|string $db 'mongodb'

Method Details

Hide inherited methods

getCollection() protected method

Returns collection used by this fixture.

protected yii\mongodb\Collection getCollection ( )
return yii\mongodb\Collection

Related collection.

                protected function getCollection()
{
    return $this->db->getCollection($this->getCollectionName());
}

            
getCollectionName() protected method

Returns collection name used by this fixture.

protected array|string getCollectionName ( )
return array|string

Related collection name

                protected function getCollectionName()
{
    if ($this->collectionName) {
        return $this->collectionName;
    }
    /* @var $modelClass ActiveRecord */
    $modelClass = $this->modelClass;
    return $modelClass::collectionName();
}

            
getData() protected method

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 collection.

If the data file does not exist, an empty array will be returned.

protected array getData ( )
return array

The data rows to be inserted into the collection.

                protected function getData()
{
    if ($this->dataFile === null) {
        $class = new \ReflectionClass($this);
        $collectionName = $this->getCollectionName();
        $dataFile = dirname($class->getFileName()) . '/data/' . (is_array($collectionName) ? implode('.', $collectionName) : $collectionName) . '.php';
        return is_file($dataFile) ? require($dataFile) : [];
    }
    return parent::getData();
}

            
init() public method

public void init ( )

                public function init()
{
    parent::init();
    if (!isset($this->modelClass) && !isset($this->collectionName)) {
        throw new InvalidConfigException('Either "modelClass" or "collectionName" must be set.');
    }
}

            
load() public method

Loads the fixture data.

The default implementation will first reset the MongoDB collection and then populate it with the data returned by getData().

public void load ( )

                public function load()
{
    $this->resetCollection();
    $this->data = [];
    $data = $this->getData();
    if (empty($data)) {
        return;
    }
    $this->getCollection()->batchInsert($data);
    foreach ($data as $alias => $row) {
        $this->data[$alias] = $row;
    }
}

            
resetCollection() protected method

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 void resetCollection ( )

                protected function resetCollection()
{
    $this->getCollection()->remove();
}