/** * @return \yii\db\ActiveQuery */ public function getTo() { return $this->hasMany(ReceivedDirection::className(), ['city_to_code' => 'code']); }
public function actionDirection() { $date_id = false; $airline = ReceivedAirline::find()->select(['iata'])->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['iata'], Console::FG_GREEN); try { $data = TravelPayoutsApi::getAirlineDirections($value['iata'], 1000); } catch (Exception $e) { $error++; $this->stdout($value['iata'] . ' ', 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 = ReceivedDirection::find()->where(['iata_code' => $value['iata'], '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 { if ($date_id === false) { $received_date = new ReceivedDate(); $received_date->created_at = date("Y-m-d H:i:s"); $received_date->received = 0; /** @var ActiveRecord $received_date */ if (!$received_date->save()) { print_r($received_date->attributes); print_r($received_date->errors); return Controller::EXIT_CODE_ERROR; } $date_id = Yii::$app->db->lastInsertID; $this->stdout(' [create new received date: ' . $received_date->created_at . '] ', Console::FG_CYAN); } $top = new ReceivedDirection(); $top->iata_code = $value['iata']; $top->city_from_code = $iata[0]; $top->city_to_code = $iata[1]; $top->popularity = $popular; $top->date_id = $date_id; /** * 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); } } } if (!empty($received_date)) { $received_date->received = $count; $received_date->updated = $update; $received_date->errors = $error; $received_date->save(); } $this->stdout(PHP_EOL . 'Added: ' . $count . ' Updates: ' . $update . ' Error ' . $error . PHP_EOL, Console::FG_BLUE); $count_iata--; } if (!empty($received_date)) { $received_date->received = $count; $received_date->updated = $update; $received_date->errors = $error; $received_date->save(); } $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 getReceivedDirections() { return $this->hasMany(ReceivedDirection::className(), ['date_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getDirections() { return $this->hasMany(ReceivedDirection::className(), ['iata_code' => 'iata']); }
public static function getDirection($params = []) { // VarDumper::dump($params,1); $params['language_id'] = TFA::$info['language_id']; self::$params = array_merge(self::$params, $params); $query = ReceivedDirection::find()->asArray(); $dataProvider = new ActiveDataProvider(['query' => $query->with('iata')->with(['from' => function ($query) { /** @var $query \yii\db\ActiveQuery */ if (!empty(self::$params['language_id'])) { $query->with(['translations' => function ($query) { /** @var $query \yii\db\ActiveQuery */ $query->andFilterWhere(['language_id' => self::$params['language_id']])->indexBy('language_id'); }]); } $query->with(['country' => function ($query) { /** @var $query \yii\db\ActiveQuery */ if (!empty(self::$params['language_id'])) { $query->with(['translations' => function ($query) { /** @var $query \yii\db\ActiveQuery */ $query->andFilterWhere(['language_id' => self::$params['language_id']])->indexBy('language_id'); }]); } }]); // $query->with(['airports' => function ($query) { // /** @var $query \yii\db\ActiveQuery */ // if (!empty(self::$params['language_id'])) { // $query->with(['translations' => function ($query) { // /** @var $query \yii\db\ActiveQuery */ // $query->andFilterWhere(['language_id' => self::$params['language_id']])->indexBy('language_id'); // }]); // } // }]); }])->with(['to' => function ($query) { /** @var $query \yii\db\ActiveQuery */ if (!empty(self::$params['language_id'])) { $query->with(['translations' => function ($query) { /** @var $query \yii\db\ActiveQuery */ $query->andFilterWhere(['language_id' => self::$params['language_id']])->indexBy('language_id'); }]); } $query->with(['country' => function ($query) { /** @var $query \yii\db\ActiveQuery */ if (!empty(self::$params['language_id'])) { $query->with(['translations' => function ($query) { /** @var $query \yii\db\ActiveQuery */ $query->andFilterWhere(['language_id' => self::$params['language_id']])->indexBy('language_id'); }]); } }]); // $query->with(['airports' => function ($query) { // /** @var $query \yii\db\ActiveQuery */ // if (!empty(self::$params['language_id'])) { // $query->with(['translations' => function ($query) { // /** @var $query \yii\db\ActiveQuery */ // $query->andFilterWhere(['language_id' => self::$params['language_id']])->indexBy('language_id'); // }]); // } // }]); }]), 'pagination' => ['pageSize' => self::$params['pageSize'], 'pageSizeParam' => false], 'sort' => ['defaultOrder' => ['popularity' => SORT_DESC], 'attributes' => ['popularity', 'iata_code', 'city_to_code']]]); if (!empty(self::$params['country_from_alias'])) { $county = ReceivedCity::find()->select(['t1.code'])->asArray()->from(['t1' => 'received_city', 't2' => 'received_country'])->where('t1.country_code = t2.code AND t2.alias_' . TFA::$info['language']['code'] . '=:alias', [':alias' => self::$params['country_from_alias']])->all(); $city_code = []; foreach ($county as $value) { $city_code[] = $value['code']; } $city_code = array_unique($city_code); $query->andFilterWhere(['city_from_code' => $city_code]); } elseif (!empty(self::$params['city_from_alias'])) { $city = ReceivedCity::find()->select('code')->where(['alias_' . TFA::$info['language']['code'] => self::$params['city_from_alias']])->asArray()->all(); $city_code = []; foreach ($city as $value) { $city_code[] = $value['code']; } $city_code = array_unique($city_code); if ($city_code) { $query->andFilterWhere(['city_from_code' => $city_code]); } } elseif (!empty(self::$params['city_from_code'])) { $query->andFilterWhere(['city_from_code' => self::$params['city_from_code']]); } if (!empty(self::$params['country_to_alias'])) { $county = ReceivedCity::find()->select(['t1.code'])->asArray()->from(['t1' => 'received_city', 't2' => 'received_country'])->where('t1.country_code = t2.code AND t2.alias_' . TFA::$info['language']['code'] . '=:alias', [':alias' => self::$params['country_to_alias']])->all(); $city_code = []; foreach ($county as $value) { $city_code[] = $value['code']; } $city_code = array_unique($city_code); $query->andFilterWhere(['city_to_code' => $city_code]); } elseif (!empty(self::$params['city_to_alias'])) { $city = ReceivedCity::find()->where(['alias_' . TFA::$info['language']['code'] => self::$params['city_to_alias']])->asArray()->all(); $city_code = []; foreach ($city as $value) { $city_code[] = $value['code']; } $city_code = array_unique($city_code); if ($city_code) { $query->andFilterWhere(['city_to_code' => $city_code]); } } elseif (!empty(self::$params['city_to_code'])) { $query->andFilterWhere(['city_to_code' => self::$params['city_to_code']]); } if (!empty(self::$params['iata_code'])) { $query->andFilterWhere(['iata_code' => self::$params['iata_code']]); } if (self::$params['direction_all'] === false) { $query->groupBy(['city_to_code', 'city_from_code']); } $dataProvider->query = $query; return $dataProvider; }