Exemplo n.º 1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getStationEconomies()
 {
     return $this->hasMany(StationEconomy::className(), ['station_id' => 'id']);
 }
 /**
  * The object parser for EDDB stations
  * @return function
  */
 private function getStationsObjectParser()
 {
     return function ($obj) {
         // Remove keys we don't want, and extract them as variables to the symbols table
         $exportKeys = ['economies', 'listings', 'import_commodities', 'export_commodities', 'prohibited_commodities', 'updated_at'];
         foreach ($exportKeys as $key) {
             ${$key} = $obj[0][$key];
             unset($obj[0][$key]);
         }
         $model = Station::find()->where(['id' => $obj[0]['id']])->one();
         if ($model === NULL) {
             $model = new Station();
         } else {
             if ($model->updated_at + 43200 >= time()) {
                 // If the model is less than 12 hours old, skip it.
                 $this->stdOut('.');
                 return;
             }
         }
         $this->stdOut('Importing station: ');
         $this->stdOut("{$obj[0]['name']} :: {$obj[0]['id']}\n", Console::BOLD);
         // Update the stations listing
         foreach ($obj[0] as $name => $value) {
             if ($model->hasAttribute($name)) {
                 $model->{$name} = $value;
             }
         }
         $model->save();
         $db = Yii::$app->db;
         // Update the economies listing
         $db->createCommand('DELETE FROM station_economies WHERE station_id = :station_id')->bindValue(':station_id', $obj[0]['id'])->execute();
         foreach ($economies as $economy) {
             $model = new StationEconomy();
             $model->attributes = ['station_id' => $obj[0]['id'], 'name' => $economy];
             $model->save();
         }
         foreach (['listings', 'import_commodities', 'export_commodities', 'prohibited_commodities'] as $k) {
             $this->importStationCommodity($obj[0], $k, new StationCommodity(), ${$k});
         }
     };
 }