예제 #1
0
 /**
  * Creates a new City model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new City();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->Id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public function up()
 {
     $cities = (include Yii::getAlias('@common') . '/data/city-list.php');
     foreach ($cities as $name) {
         $city = new City();
         $city->name = $name;
         $city->save();
     }
 }
예제 #3
0
 public function actionCity()
 {
     $this->stdout('Request API: Cities.', Console::FG_GREEN);
     $data = TravelPayoutsApi::getCities();
     $_count = count($data);
     $this->stdout(' Get: ' . $_count . ' Cities.' . PHP_EOL, Console::FG_GREEN);
     Console::startProgress(0, $_count);
     $count = 0;
     $count_ = 0;
     foreach ($data as $value) {
         $value->lon = !empty($value->coordinates->lon) ? (string) $value->coordinates->lon : '';
         $value->lat = !empty($value->coordinates->lat) ? (string) $value->coordinates->lat : '';
         $name_translations = $value->name_translations;
         unset($value->name_translations);
         unset($value->coordinates);
         $city = City::find()->where(['code' => $value->code])->one();
         if (!$city) {
             $this->stdout($value->code . ' ', Console::FG_YELLOW);
             $city = new City();
             $city->attributes = (array) $value;
             $local = ['en-GB', 'en-AU', 'en-CA', 'en-NZ', 'en-IE', 'en-SG', 'en-IN'];
             foreach ($name_translations as $_key => $_name) {
                 if (array_search($_key, $local) === false) {
                     $city['name_' . $_key] = trim($_name);
                 }
             }
             if (!$city->save()) {
                 print_r($value);
                 print_r($city->errors);
                 return Controller::EXIT_CODE_ERROR;
             }
             $count++;
         } else {
             Console::updateProgress(++$count_, $_count);
         }
     }
     Console::endProgress();
     $this->stdout(PHP_EOL . 'Added: ' . $count . ' Cities.' . PHP_EOL, Console::FG_BLUE);
     return Controller::EXIT_CODE_NORMAL;
 }