public function actionDirection() { $airline = Airline::find()->select(['code'])->asArray()->all(); if ($airline) { $count = 0; $update = 0; $error = 0; $count_iata = count($airline); $this->stdout('Get IATA: ' . $count_iata . PHP_EOL, Console::FG_GREEN); foreach ($airline as $value) { $this->stdout('[' . $count_iata . '] Request API: Country IATA: ' . $value['code'], Console::FG_GREEN); try { $data = TravelPayoutsApi::getAirlineDirections($value['code'], 1000); } catch (Exception $e) { $error++; $this->stdout($value['code'] . ' ', Console::FG_RED); print_r($e); die; } $this->stdout(', Get: ' . count($data) . ' Directions.' . PHP_EOL, Console::FG_GREEN); foreach ($data as $cities => $popular) { $iata = explode('-', $cities); $top = Direction::find()->where(['iata_code' => $value['code'], 'city_from_code' => $iata[0], 'city_to_code' => $iata[1]])->one(); if ($top) { if ($top->popularity != $popular) { $this->stdout($cities . ' ', Console::FG_BLUE); $top->popularity = $popular; if (!$top->save()) { print_r($top->attributes); print_r($top->errors); return Controller::EXIT_CODE_ERROR; } $update++; } } else { $top = new Direction(); $top->iata_code = $value['code']; $top->city_from_code = $iata[0]; $top->city_to_code = $iata[1]; $top->popularity = $popular; /** * todo проблема - в базе не существуют города или неправильный развер ключа, например вместо кода написано "Москва" */ try { if (!$top->save()) { $this->stdout($cities . ' ', Console::FG_RED); print_r($top->attributes); print_r($top->errors); $error++; } else { $count++; $this->stdout($cities . ' ', Console::FG_YELLOW); } } catch (Exception $e) { $error++; $this->stdout($cities . ' ', Console::FG_RED); } } } $this->stdout(PHP_EOL . 'Added: ' . $count . ' Updates: ' . $update . ' Error ' . $error . PHP_EOL, Console::FG_BLUE); $count_iata--; } $this->stdout(PHP_EOL . 'Added: ' . $count . ' Updates: ' . $update . ' Error ' . $error . PHP_EOL, Console::FG_BLUE); return Controller::EXIT_CODE_NORMAL; } $this->stdout(PHP_EOL . 'No ReceivedAirline database' . PHP_EOL, Console::FG_RED); return Controller::EXIT_CODE_ERROR; }
/** * @return \yii\db\ActiveQuery */ public function getIataCode() { return $this->hasOne(Airline::className(), ['code' => 'iata_code']); }