1 | <?php |
||
20 | abstract class BaseActiveFixture extends DbFixture implements \IteratorAggregate, \ArrayAccess, \Countable |
||
21 | { |
||
22 | use ArrayAccessTrait; |
||
23 | |||
24 | /** |
||
25 | * @var string the AR model class associated with this fixture. |
||
26 | */ |
||
27 | public $modelClass; |
||
28 | /** |
||
29 | * @var array the data rows. Each array element represents one row of data (column name => column value). |
||
30 | */ |
||
31 | public $data = []; |
||
32 | /** |
||
33 | * @var string|boolean the file path or path alias of the data file that contains the fixture data |
||
34 | * to be returned by [[getData()]]. You can set this property to be false to prevent loading any data. |
||
35 | */ |
||
36 | public $dataFile; |
||
37 | |||
38 | /** |
||
39 | * @var \yii\db\ActiveRecord[] the loaded AR models |
||
40 | */ |
||
41 | private $_models = []; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Returns the AR model by the specified model name. |
||
46 | * A model name is the key of the corresponding data row in [[data]]. |
||
47 | * @param string $name the model name. |
||
48 | * @return null|\yii\db\ActiveRecord the AR model, or null if the model cannot be found in the database |
||
49 | * @throws \yii\base\InvalidConfigException if [[modelClass]] is not set. |
||
50 | */ |
||
51 | public function getModel($name) |
||
52 | { |
||
53 | if (!isset($this->data[$name])) { |
||
54 | return null; |
||
55 | } |
||
56 | if (array_key_exists($name, $this->_models)) { |
||
57 | return $this->_models[$name]; |
||
58 | } |
||
59 | |||
60 | if ($this->modelClass === null) { |
||
61 | throw new InvalidConfigException('The "modelClass" property must be set.'); |
||
62 | } |
||
63 | $row = $this->data[$name]; |
||
64 | /* @var $modelClass \yii\db\ActiveRecord */ |
||
65 | $modelClass = $this->modelClass; |
||
66 | /* @var $model \yii\db\ActiveRecord */ |
||
67 | $model = new $modelClass; |
||
68 | $keys = []; |
||
69 | foreach ($model->primaryKey() as $key) { |
||
70 | $keys[$key] = isset($row[$key]) ? $row[$key] : null; |
||
71 | } |
||
72 | |||
73 | return $this->_models[$name] = $modelClass::findOne($keys); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Loads the fixture. |
||
78 | * |
||
79 | * The default implementation simply stores the data returned by [[getData()]] in [[data]]. |
||
80 | * You should usually override this method by putting the data into the underlying database. |
||
81 | */ |
||
82 | public function load() |
||
86 | |||
87 | /** |
||
88 | * Returns the fixture data. |
||
89 | * |
||
90 | * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]]. |
||
91 | * The file should return the data array that will be stored in [[data]] after inserting into the database. |
||
92 | * |
||
93 | * @return array the data to be put into the database |
||
94 | * @throws InvalidConfigException if the specified data file does not exist. |
||
95 | */ |
||
96 | protected function getData() |
||
108 | |||
109 | /** |
||
110 | * @inheritdoc |
||
111 | */ |
||
112 | public function unload() |
||
118 | } |
||
119 |