/** * Creates a new Station model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Station(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->station_id]); } else { return $this->render('create', ['model' => $model]); } }
public function store(Request $request) { $inputsData = $request->only('name', 'state'); $newStation = new Station($inputsData); if ($newStation->save()) { return response()->json($newStation); } else { return response()->validation_error($newStation->errors(), 'Estacion no ha sido registrada'); } }
/** * 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}); } }; }