Esempio n. 1
0
 /**
  * @return \yii\db\ActiveRelation
  */
 public function getTimezone(array $timezones = [])
 {
     $region = $this->region;
     if (empty($region)) {
         $location = self::find()->fromPoint((double) $this->latitude, (double) $this->longitude, self::FIND_REGION_DISTANCE)->andWhere('region IS NOT NULL AND region <> \'\'')->andWhere('country = :country', [':country' => $this->country])->limit(1)->one();
         $region = $location->region;
     }
     $query = Timezones::find()->where('country = :country', [':country' => $this->country]);
     if (!empty($region)) {
         $cloned = clone $query;
         $cloned->andWhere('region = :region', [':region' => $region]);
         $cloned->orderBy('region DESC');
         if ($cloned->exists()) {
             $query = $cloned;
         }
     }
     if (!empty($timezones)) {
         $query->andWhere(['timezone' => $timezones]);
     }
     return $query;
 }
Esempio n. 2
0
 protected function applyCsv($file)
 {
     $dataColumns = ['country', 'region', 'timezone', 'create_time', 'update_time'];
     $dataDuplicates = ['country', 'region', 'timezone'];
     $dataRows = [];
     if (($handle = fopen($file, 'r')) !== false) {
         while (($data = fgetcsv($handle, 1000, ',')) !== false) {
             if (!isset($data[0]) || count($data) !== 3 || $data[0] === 'country') {
                 continue;
             }
             $dataRows[] = [trim($data[0]), trim($data[1]), trim($data[2]), time(), time()];
             if (count($dataRows) === $this->maxExecuteRows) {
                 $this->batchInsertDuplicate(Timezones::tableName(), $dataColumns, $dataRows, $dataDuplicates)->execute();
                 $dataRows = [];
             }
         }
         if (count($dataRows) > 0) {
             $this->batchInsertDuplicate(Timezones::tableName(), $dataColumns, $dataRows, $dataDuplicates)->execute();
         }
         fclose($handle);
     }
 }
 public function down()
 {
     $this->dropTable(Timezones::tableName());
 }