Class yii\sphinx\ActiveFixture

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

ActiveFixture represents a fixture backed up by a modelClass or a Sphinx index.

Either modelClass or $indexName 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 resetIndex() to remove any existing data in the index. It will then populate the index 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().

Note: only runtime indexes are supported.

Public Properties

Hide inherited properties

Property Type Description Defined By
$dataFile string|boolean The file path or path alias of the data file that contains the fixture data to be returned by getData(). yii\sphinx\ActiveFixture
$db yii\sphinx\Connection|array|string The Sphinx connection object or the application component ID of the Sphinx connection or a configuration array for creating the object. yii\sphinx\ActiveFixture
$indexName string The name of the Sphinx index that this fixture is about. yii\sphinx\ActiveFixture
$indexSchema yii\sphinx\IndexSchema The schema information of the database table associated with this fixture. yii\sphinx\ActiveFixture

Protected Methods

Hide inherited methods

Method Description Defined By
getData() Returns the fixture data. yii\sphinx\ActiveFixture
resetIndex() Truncates the specified index removing all existing data from it. yii\sphinx\ActiveFixture

Property Details

Hide inherited properties

$dataFile public property

The file path or path alias of the data file that contains the fixture data to be returned by getData(). If this is not set, it will default to FixturePath/data/IndexName.php, where FixturePath stands for the directory containing this fixture class, and IndexName stands for the name of the index associated with this fixture. You can set this property to be false to prevent loading any data.

public string|boolean $dataFile null
$db public property

The Sphinx connection object or the application component ID of the Sphinx connection or a configuration array for creating the object.

$indexName public property

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

See also \yii\sphinx\modelClass.

public string $indexName null
$indexSchema public property

The schema information of the database table associated with this fixture.

Method Details

Hide inherited methods

getData() protected method

Returns the 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 index.

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

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

            
getIndexSchema() public method

public yii\sphinx\IndexSchema getIndexSchema ( )
return yii\sphinx\IndexSchema

The schema information of the database table associated with this fixture.

throws \yii\base\InvalidConfigException

if the index does not exist or not a runtime type

                public function getIndexSchema()
{
    if ($this->_index !== null) {
        return $this->_index;
    }
    $db = $this->db;
    $indexName = $this->indexName;
    if ($indexName === null) {
        /* @var $modelClass ActiveRecord */
        $modelClass = $this->modelClass;
        $indexName = $modelClass::indexName();
    }
    $this->_index = $db->getSchema()->getIndexSchema($indexName);
    if ($this->_index === null) {
        throw new InvalidConfigException("Index does not exist: {$indexName}");
    }
    if (!$this->_index->isRt) {
        throw new InvalidConfigException("'{$indexName}' is not a runtime index. Only runtime indexes are supported.'");
    }
    return $this->_index;
}

            
init() public method

public void init ( )

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

            
load() public method

Loads the fixture.

The default implementation will first clean up the table by calling resetIndex(). It will then populate the index with the data returned by getData().

If you override this method, you should consider calling the parent implementation so that the data returned by getData() can be populated into the index.

public void load ( )

                public function load()
{
    $this->resetIndex();
    $this->data = [];
    $index = $this->getIndexSchema();
    foreach ($this->getData() as $alias => $row) {
        $this->db->createCommand()->insert($index->name, $row)->execute();
        $this->data[$alias] = $row;
    }
}

            
resetIndex() protected method

Truncates the specified index removing all existing data from it.

This method is called before populating fixture data into the index associated with this fixture.

protected void resetIndex ( )

                protected function resetIndex()
{
    $index = $this->getIndexSchema();
    $this->db->createCommand()->truncateIndex($index->name)->execute();
}