| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created for IG Monitoring. |
||
| 4 | * User: jakim <[email protected]> |
||
| 5 | * Date: 20.08.2018 |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace app\commands; |
||
| 9 | |||
| 10 | |||
| 11 | use app\dictionaries\AccountInvalidationType; |
||
| 12 | use app\dictionaries\TagInvalidationType; |
||
| 13 | use yii\console\Controller; |
||
| 14 | use yii\console\ExitCode; |
||
| 15 | use yii\helpers\Console; |
||
| 16 | use yii\helpers\StringHelper; |
||
| 17 | |||
| 18 | class AdminController extends Controller |
||
| 19 | { |
||
| 20 | public function actionDictionaries() |
||
| 21 | { |
||
| 22 | $dictionaries = [ |
||
| 23 | AccountInvalidationType::class, |
||
| 24 | TagInvalidationType::class, |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** @var \app\dictionaries\Dictionary $dictionary */ |
||
| 28 | foreach ($dictionaries as $dictionary) { |
||
| 29 | $this->stdout("$dictionary\n"); |
||
| 30 | |||
| 31 | $modelClass = '\\app\\models\\' . StringHelper::basename($dictionary); |
||
| 32 | if (!class_exists($modelClass)) { |
||
| 33 | $this->stderr("ERR: '$modelClass'\n"); |
||
| 34 | |||
| 35 | return ExitCode::UNSPECIFIED_ERROR; |
||
| 36 | } |
||
| 37 | |||
| 38 | foreach ($dictionary::labels() as $id => $name) { |
||
| 39 | /** @var \yii\db\ActiveRecord $modelClass */ |
||
| 40 | $model = $modelClass::findOne($id); |
||
| 41 | if ($model === null) { |
||
| 42 | $model = new $modelClass(); |
||
| 43 | $model->id = $id; |
||
| 44 | } |
||
| 45 | $model->name = $name; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 46 | /** @var \yii\db\ActiveRecord $model */ |
||
| 47 | if (!$model->save()) { |
||
| 48 | echo Console::errorSummary($model); |
||
| 49 | |||
| 50 | return ExitCode::DATAERR; |
||
| 51 | } |
||
| 52 | $this->stdout("$id => $name\n"); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | } |
||
| 57 | } |