0 follower

Trait yii\test\FileFixtureTrait

Implemented byyii\test\ActiveFixture, yii\test\ArrayFixture, yii\test\BaseActiveFixture
Available since version2.0.14
Source Codehttps://.com/yiisoft/yii2/blob/master/framework/test/FileFixtureTrait.php

FileFixtureTrait provides functionalities for loading data fixture from file.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$dataDirectorystringThe directory path or path alias that contains the fixture datayii\test\FileFixtureTrait
$dataFilestring|booleanThe file path or path alias of the data file that contains the fixture data to be returned by getData().yii\test\FileFixtureTrait

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
loadData()Returns the fixture data.yii\test\FileFixtureTrait

Property Details

Hide inherited properties

$dataDirectory public property

The directory path or path alias that contains the fixture data

public string $dataDirectory null
$dataFile public property

The file path or path alias of the data file that contains the fixture data to be returned by getData(). You can set this property to be false to prevent loading any data.

public string|boolean $dataFile null

Method Details

Hide inherited methods

loadData() 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 the data array that will be stored in data after inserting into the database.

protected array loadData ( $file, $throwException true )
$filestring

The data file path

$throwExceptionboolean

Whether to throw exception if fixture data file does not exist.

returnarray

The data to be put into the database

throwsyii\base\InvalidConfigException

if the specified data file does not exist.

                protected function loadData($file, $throwException = true)
{
    if ($file === null || $file === false) {
        return [];
    }
    if (basename($file) === $file && $this->dataDirectory !== null) {
        $file = $this->dataDirectory . '/' . $file;
    }
    $file = Yii::getAlias($file);
    if (is_file($file)) {
        return require $file;
    }
    if ($throwException) {
        throw new InvalidConfigException("Fixture data file does not exist: {$file}");
    }
    return [];
}