Trait yii\test\FileFixtureTrait
實作於 | yii\test\ActiveFixture, yii\test\ArrayFixture, yii\test\BaseActiveFixture |
---|---|
自版本 | 2.0.14 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/test/FileFixtureTrait.php |
FileFixtureTrait 提供了從檔案載入資料 fixture 的功能。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$dataDirectory | string | 包含 fixture 資料的目錄路徑或 路徑別名 | yii\test\FileFixtureTrait |
$dataFile | string|boolean | 包含 fixture 資料的檔案路徑或 路徑別名,將由 getData() 返回。 | yii\test\FileFixtureTrait |
屬性詳情
方法詳情
返回 fixture 資料。
預設實作會嘗試透過引入 $dataFile 指定的外部檔案來傳回 fixture 資料。該檔案應傳回資料陣列,此陣列將在插入資料庫後儲存在 data 中。
protected array loadData ( $file, $throwException = true ) | ||
$file | string |
資料檔案路徑 |
$throwException | boolean(布林值) |
若 fixture 資料檔案不存在時是否拋出例外。 |
return(回傳) | array(陣列) |
要放入資料庫的資料 |
---|---|---|
throws(拋出) | yii\base\InvalidConfigException |
若指定的資料檔案不存在。 |
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 [];
}
註冊 或 登入 以進行評論。