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