public static function findRemote($uid) { $model = self::findOne(['uid' => $uid]); if (!isset($model)) { $model = new City(['uid' => $uid]); $model->syncRemote(); } return $model; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $this->load($params); $query = City::find(); $query->select(['*', 'name' => 'city_translation.name', 'language' => 'city.language_id']); $query->leftJoin('city_translation', 'city_translation.city_id=city.id'); //$query->andWhere(['city.language_id' => 'city_translation.language_id']); // $query->joinWith('city'); // $query->joinWith('city.localisedIdentification'); if (isset($this->uid)) { $this->pageSize = 1; } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => $this->pagination ? ['pageSize' => $this->pageSize] : FALSE, 'sort' => ['attributes' => ['name', 'postcode', 'language']]]); if (isset($this->uid)) { // uncomment the following line if you do not want to return any records when validation fails $query->where(['uid' => $this->uid]); return $dataProvider; } if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails $query->where('0=1'); return $dataProvider; } return $dataProvider; }
public function download() { //Make Connection - Ensure that Connection Parameters exist if (!isset($this->uid)) { throw new \yii\base\InvalidConfigException("Model UID must be set for remote synchronisation"); } $raw = (new Viajero())->get('places', ['query' => ['uid' => $this->uid]])->getBody(); $formatted = Json::decode($raw, true); // \yii\helpers\VarDumper::dump($formatted); if (count($formatted == 1)) { $data = $formatted[0]; if (!isset($data['place']) || !isset($data['place']['uid'])) { throw new \UnexpectedValueException('Location: syncRemote - Unexpected Data format'); } $model = City::findRemote($data['place']['uid']); if (isset($model)) { $this->city_id = $model->id; $this->country_id = $data['country']; $this->postcode = $data['postcode']; $this->save(); } } }
private function _dummyLocation($countryId, $name, $postCode) { //CityTranslation::find()->where(['language_id' => 'EN', 'name' => $name])->queryScalar(); $dummyCity = new \humanized\location\models\location\City(['language_id' => 'EN']); try { $dummyCity->save(); try { $dummyCityTranslation = new \humanized\location\models\translation\CityTranslation(['language_id' => 'EN', 'city_id' => $dummyCity->id, 'name' => $name]); $dummyCityTranslation->save(); try { $dummyLocation = new \humanized\location\models\location\Location(['postcode' => $postCode, 'city_id' => $dummyCity->id, 'country_id' => $countryId]); $dummyLocation->save(); } catch (Exception $ex) { } } catch (Exception $ex) { } } catch (\Exception $ex) { } }
public function actionDefault($fn) { if (strlen($fn) != 5) { $this->stderror('filename like <country-code>_<language-code> (5 characters - ommit .csv extension)' . "\n"); } //if filename is not 5 characters long, throw an error and display filename model $fileName = \Yii::getAlias('@data') . "/location/{$fn}.csv"; //resolve file url from /location/ $file = fopen($fileName, "r"); $countryCode = strtoupper(substr($fn, 0, 2)); $languageCode = strtoupper(substr($fn, 3, 2)); $this->stdout('Importing City Data for ' . $countryCode . ' in language ' . $languageCode . "\n"); //fgetcsv($file, 0); while (!feof($file)) { $record = fgetcsv($file, 0, ';'); if (isset($record[0])) { $city = new City(['language_id' => $languageCode, 'local_name' => $record[1]]); if (!$city->save()) { \yii\helpers\VarDumper::dump($city->errors); } // (new CityTranslation(['name' => $record[1], 'city_id' => $city->id, 'language_id' => $languageCode]))->save(); $location = new Location(['city_id' => $city->id, 'country_id' => $countryCode, 'postcode' => $record[0]]); if (!$location->save()) { echo $location->city_id . '=' . $location->country_id . '=' . $location->uid . '=' . $location->postcode; \yii\helpers\VarDumper::dump($location->errors); } } else { break; } } }
/** * @return \yii\db\ActiveQuery */ public function getCity() { return $this->hasOne(City::className(), ['id' => 'city_id']); }
public static function getCityList($params) { return City::find()->asArray()->all(); }