Commit 3cc7fcb5 by Qiang Xue

refactored fixture.

parent 5c3bd6ab
...@@ -100,16 +100,13 @@ class ActiveFixture extends BaseActiveFixture ...@@ -100,16 +100,13 @@ class ActiveFixture extends BaseActiveFixture
*/ */
protected function getData() protected function getData()
{ {
if ($this->dataFile === false) { if ($this->dataFile === null) {
return [];
}
if ($this->dataFile !== null) {
$dataFile = Yii::getAlias($this->dataFile);
} else {
$class = new \ReflectionClass($this); $class = new \ReflectionClass($this);
$dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php'; $dataFile = dirname($class->getFileName()) . '/data/' . $this->getTableSchema()->fullName . '.php';
}
return is_file($dataFile) ? require($dataFile) : []; return is_file($dataFile) ? require($dataFile) : [];
} else {
return parent::getData();
}
} }
/** /**
......
...@@ -31,6 +31,11 @@ abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate ...@@ -31,6 +31,11 @@ abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate
*/ */
public $data = []; public $data = [];
/** /**
* @var string|boolean 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 $dataFile;
/**
* @var \yii\db\ActiveRecord[] the loaded AR models * @var \yii\db\ActiveRecord[] the loaded AR models
*/ */
private $_models = []; private $_models = [];
...@@ -66,4 +71,37 @@ abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate ...@@ -66,4 +71,37 @@ abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate
} }
return $this->_models[$name] = $modelClass::find($keys); return $this->_models[$name] = $modelClass::find($keys);
} }
/**
* Loads the fixture.
*
* The default implementation simply stores the data returned by [[getData()]] in [[data]].
* You should usually override this method by putting the data into the underlying database.
*/
public function load()
{
$this->data = $this->getData();
}
/**
* 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.
*
* @return array the data to be put into the database
* @throws InvalidConfigException if the specified data file does not exist.
*/
protected function getData()
{
if ($this->dataFile === false || $this->dataFile === null) {
return [];
}
$dataFile = Yii::getAlias($this->dataFile);
if (is_file($dataFile)) {
return require($dataFile);
} else {
throw new InvalidConfigException("Fixture data file does not exist: {$this->dataFile}");
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment