1 | <?php |
||
34 | class ActiveFixture extends BaseActiveFixture |
||
35 | { |
||
36 | /** |
||
37 | * @var string the name of the database table that this fixture is about. If this property is not set, |
||
38 | * the table name will be determined via [[modelClass]]. |
||
39 | * @see modelClass |
||
40 | */ |
||
41 | public $tableName; |
||
42 | /** |
||
43 | * @var string|bool the file path or [path alias](guide:concept-aliases) of the data file that contains the fixture data |
||
44 | * to be returned by [[getData()]]. If this is not set, it will default to `FixturePath/data/TableName.php`, |
||
45 | * where `FixturePath` stands for the directory containing this fixture class, and `TableName` stands for the |
||
46 | * name of the table associated with this fixture. You can set this property to be false to prevent loading any data. |
||
47 | */ |
||
48 | public $dataFile; |
||
49 | |||
50 | /** |
||
51 | * @var TableSchema the table schema for the table associated with this fixture |
||
52 | */ |
||
53 | private $_table; |
||
54 | |||
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | 8 | public function init() |
|
66 | |||
67 | /** |
||
68 | * Loads the fixture. |
||
69 | * |
||
70 | * The default implementation will first clean up the table by calling [[resetTable()]]. |
||
71 | * It will then populate the table with the data returned by [[getData()]]. |
||
72 | * |
||
73 | * If you override this method, you should consider calling the parent implementation |
||
74 | * so that the data returned by [[getData()]] can be populated into the table. |
||
75 | */ |
||
76 | 8 | public function load() |
|
77 | { |
||
78 | 8 | $this->data = []; |
|
79 | 8 | $table = $this->getTableSchema(); |
|
80 | 8 | foreach ($this->getData() as $alias => $row) { |
|
81 | 8 | $primaryKeys = $this->db->schema->insert($table->fullName, $row); |
|
82 | 8 | $this->data[$alias] = array_merge($row, $primaryKeys); |
|
83 | } |
||
84 | 8 | } |
|
85 | |||
86 | /** |
||
87 | * Returns the fixture data. |
||
88 | * |
||
89 | * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]]. |
||
90 | * The file should return an array of data rows (column name => column value), each corresponding to a row in the table. |
||
91 | * |
||
92 | * If the data file does not exist, an empty array will be returned. |
||
93 | * |
||
94 | * @return array the data rows to be inserted into the database table. |
||
95 | */ |
||
96 | 8 | protected function getData() |
|
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | 8 | public function unload() |
|
116 | |||
117 | /** |
||
118 | * Removes all existing data from the specified table and resets sequence number to 1 (if any). |
||
119 | * This method is called before populating fixture data into the table associated with this fixture. |
||
120 | */ |
||
121 | 8 | protected function resetTable() |
|
129 | |||
130 | /** |
||
131 | * @return TableSchema the schema information of the database table associated with this fixture. |
||
132 | * @throws \yii\base\InvalidConfigException if the table does not exist |
||
133 | */ |
||
134 | 8 | public function getTableSchema() |
|
155 | } |
||
156 |