Exemplo n.º 1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Country::find();
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     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;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'iso_numeric' => $this->iso_numeric, 'area' => $this->area, 'population' => $this->population, 'geoname_id' => $this->geoname_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'iso', $this->iso])->andFilterWhere(['like', 'iso3', $this->iso3])->andFilterWhere(['like', 'fips', $this->fips])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'capital', $this->capital])->andFilterWhere(['like', 'continent', $this->continent])->andFilterWhere(['like', 'tld', $this->tld])->andFilterWhere(['like', 'currency_code', $this->currency_code])->andFilterWhere(['like', 'currency_name', $this->currency_name])->andFilterWhere(['like', 'phone_code', $this->phone_code])->andFilterWhere(['like', 'postal_code_format', $this->postal_code_format])->andFilterWhere(['like', 'postal_code_regex', $this->postal_code_regex])->andFilterWhere(['like', 'languages', $this->languages])->andFilterWhere(['like', 'neighbours', $this->neighbours])->andFilterWhere(['like', 'equivalent_fips_code', $this->equivalent_fips_code]);
     return $dataProvider;
 }
 /**
  * @see https://github.com/debuggable/php_arrays/tree/master/generators
  * @param type $file
  */
 protected function applyCsv($file)
 {
     if (($handle = fopen($file, 'r')) === false) {
         return;
     }
     for ($i = 0; ($data = fgetcsv($handle, 1000, "\t")) !== false; $i++) {
         if ($i === 0) {
             continue;
         }
         $country = Country::find()->with('translations')->where(['iso' => $data[0]])->one();
         if ($country === null) {
             continue;
         }
         $country->translate($this->language)->name = $data[4];
         $country->translate($this->language)->capital = $data[5];
         $country->translate($this->language)->currency_name = $data[10];
         if (!$country->save()) {
             throw new \yii\base\InvalidParamException('Unable to update country.');
         }
     }
     fclose($handle);
 }