1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* EDbFixtureManager represent the console command which helps you to manage your basic fixtures. |
5
|
|
|
* Available command properties: |
6
|
|
|
* pathToFixtures - path to your fixtures file |
7
|
|
|
* modelsFolder - path to folder where your models classes lay, default to `application.models.*` |
8
|
|
|
* |
9
|
|
|
* Note: |
10
|
|
|
* 1) All attributes you want to fill via fixtures data, must be defined with `safe` validation rule (`rules()` method); |
11
|
|
|
* 2) Don't forget configure `tablePrefix` option for `db` connection definition; |
12
|
|
|
* 3) Your tables will be purged when you loading fixtures; |
13
|
|
|
* |
14
|
|
|
* For more complex info about usage, see README.md |
15
|
|
|
*/ |
16
|
|
|
class EDbFixtureManager extends CConsoleCommand |
17
|
|
|
{ |
18
|
|
|
public $pathToFixtures; |
19
|
|
|
public $modelsFolder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Load fixtures into database from fixtures file |
23
|
|
|
*/ |
24
|
|
|
public function actionLoad() |
25
|
|
|
{ |
26
|
|
|
echo "\033[36m Are you sure you want to load fixtures? Your database will be purged! [Y/N] \033[0m"; |
27
|
|
|
|
28
|
|
|
$phpStdin = fopen("php://stdin", "r"); |
29
|
|
|
$inputValue = fgets($phpStdin); |
30
|
|
|
$purifiedLine = preg_replace('/[^A-Za-z0-9\-]/', '', $inputValue); |
31
|
|
|
|
32
|
|
|
if (strtolower($purifiedLine) == 'n') { |
33
|
|
|
echo "\033[34m Stopping the executing... Done. \033[0m \n"; |
34
|
|
|
die; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (empty($this->pathToFixtures) || !file_exists($this->pathToFixtures)) { |
38
|
|
|
echo "\033[33m There is no file with fixtures to load! Make sure that you create file with fixtures, |
39
|
|
|
or pass correct file name \033[0m \n"; |
40
|
|
|
die; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// import models classes to make available create new instances |
44
|
|
|
$this->importModels(); |
45
|
|
|
|
46
|
|
|
$fixtures = require_once $this->pathToFixtures; // require that file with fixtures, will be array |
47
|
|
|
$errorList = array(); |
48
|
|
|
|
49
|
|
|
foreach ($fixtures as $modelClass => $instances) { // run through the array with fixtures |
50
|
|
|
$modelClass::model()->deleteAll(); // removing old rows from database if database is not truncated |
51
|
|
|
foreach ($instances as $key => $instance) { // go through all instances for certain model, and save it into db |
52
|
|
|
$model = new $modelClass(); |
53
|
|
|
$model->attributes = $instances[$key]; |
54
|
|
|
$model->save(); |
55
|
|
|
$errorList[] = $model->getErrors(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->outputErrors($errorList); |
60
|
|
|
|
61
|
|
|
echo "\033[37;42m All fixtures loaded properly \033[0m \n"; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Show a some info about `fixtures` command |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getHelp() |
70
|
|
|
{ |
71
|
|
|
$output = "\033[34m This command will allow you to manage your fixtures in a simple way. |
72
|
|
|
Be careful all rows from database will be removed! \033[0m \n\n"; |
73
|
|
|
|
74
|
|
|
return $output . parent::getHelp(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function importModels() |
78
|
|
|
{ |
79
|
|
|
if (is_array($this->modelsFolder)) { |
80
|
|
|
foreach ($this->modelsFolder as $folder) { |
81
|
|
|
Yii::import($folder); |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
Yii::import(empty($this->modelsFolder) ? 'application.models.*' : $this->modelsFolder); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $errorList |
90
|
|
|
*/ |
91
|
|
|
private function outputErrors($errorList) |
92
|
|
|
{ |
93
|
|
|
if (empty($errorList)) { // if error list not empty |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
echo "\033[31m Validation errors occurs during loading the fixtures, some fixtures wasn't loaded to database \033[0m \n |
98
|
|
|
\033[33m The next errors occur \033[0m \n"; |
99
|
|
|
foreach ($errorList as $errors) { // run over all errors and display error what occur during saving into db |
100
|
|
|
foreach ($errors as $error) { |
101
|
|
|
foreach ($error as $value) { |
102
|
|
|
echo "\033[37;41m" . $value . "\033[0m \n"; //display error |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
die; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|