1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\test; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\ArrayAccessTrait; |
12
|
|
|
use yii\base\InvalidConfigException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* BaseActiveFixture is the base class for fixture classes that support accessing fixture data as ActiveRecord objects. |
16
|
|
|
* |
17
|
|
|
* @author Qiang Xue <[email protected]> |
18
|
|
|
* @since 2.0 |
19
|
|
|
*/ |
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
|
3 |
|
public function getModel($name) |
52
|
|
|
{ |
53
|
3 |
|
if (!isset($this->data[$name])) { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
3 |
|
if (array_key_exists($name, $this->_models)) { |
57
|
3 |
|
return $this->_models[$name]; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
if ($this->modelClass === null) { |
61
|
|
|
throw new InvalidConfigException('The "modelClass" property must be set.'); |
62
|
|
|
} |
63
|
3 |
|
$row = $this->data[$name]; |
64
|
|
|
/* @var $modelClass \yii\db\ActiveRecord */ |
65
|
3 |
|
$modelClass = $this->modelClass; |
66
|
|
|
/* @var $model \yii\db\ActiveRecord */ |
67
|
3 |
|
$model = new $modelClass; |
68
|
3 |
|
$keys = []; |
69
|
3 |
|
foreach ($model->primaryKey() as $key) { |
70
|
3 |
|
$keys[$key] = isset($row[$key]) ? $row[$key] : null; |
71
|
3 |
|
} |
72
|
|
|
|
73
|
3 |
|
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() |
83
|
|
|
{ |
84
|
|
|
$this->data = $this->getData(); |
85
|
|
|
} |
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() |
97
|
|
|
{ |
98
|
|
|
if ($this->dataFile === false || $this->dataFile === null) { |
99
|
|
|
return []; |
100
|
|
|
} |
101
|
|
|
$dataFile = Yii::getAlias($this->dataFile); |
102
|
|
|
if (is_file($dataFile)) { |
103
|
|
|
return require($dataFile); |
104
|
|
|
} else { |
105
|
|
|
throw new InvalidConfigException("Fixture data file does not exist: {$this->dataFile}"); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritdoc |
111
|
|
|
*/ |
112
|
6 |
|
public function unload() |
113
|
|
|
{ |
114
|
6 |
|
parent::unload(); |
115
|
6 |
|
$this->data = []; |
116
|
6 |
|
$this->_models = []; |
117
|
6 |
|
} |
118
|
|
|
} |
119
|
|
|
|