public function actionUssites() { $file = Yii::getAlias('@upload/_uscities.txt'); $this->stdout('Request TXT: _uscities.' . PHP_EOL, Console::FG_GREEN); if (file_exists($file)) { $handle = fopen($file, "r"); $count = 0; while (!feof($handle)) { $buffer = explode('|', trim(fgets($handle, 4096))); $state = ReceivedUsState::find()->where(['code' => $buffer[2]])->one(); if (!$state) { $this->stdout($buffer[2] . ' ', Console::FG_BLUE); $state = new ReceivedUsState(); $state->name = $buffer[1]; $state->code = $buffer[2]; $state->alias = $this->translit($buffer[1]); if (!$state->save()) { print_r($buffer); print_r($state->errors); return Controller::EXIT_CODE_ERROR; } } $city = ReceivedUsCity::find()->where(['name' => $buffer[0], 'state_code' => $buffer[2]])->one(); if (!$city) { $this->stdout($buffer[0] . ' ', Console::FG_YELLOW); $city = new ReceivedUsCity(); $city->name = $buffer[0]; $city->alias = $this->translit($buffer[0]); $city->state_code = $buffer[2]; if (!$city->save()) { print_r($buffer); print_r($city->errors); return Controller::EXIT_CODE_ERROR; } $count++; } } fclose($handle); $this->stdout(PHP_EOL . 'Added: ' . $count . ' city.' . PHP_EOL, Console::FG_BLUE); return Controller::EXIT_CODE_NORMAL; } else { $this->stdout('No file: ', Console::FG_RED); echo $file . PHP_EOL; return Controller::EXIT_CODE_ERROR; } }
/** * @return \yii\db\ActiveQuery */ public function getStateCode() { return $this->hasOne(ReceivedUsState::className(), ['code' => 'state_code']); }