Example #1
0
 protected function applyCsv($file)
 {
     $regionColumns = ['country', 'region', 'name', 'create_time', 'update_time'];
     $regionDuplicates = ['country', 'region', 'name'];
     $return = false;
     $regionRows = [];
     if (($handle = fopen($file, 'r')) !== false) {
         while (($data = fgetcsv($handle, 1000, ',')) !== false) {
             if (!isset($data[0]) || count($data) !== 3) {
                 continue;
             }
             $return = true;
             $regionRows[] = [trim($data[0]), trim($data[1]), trim($data[2]), time(), time()];
             if (count($regionRows) === $this->maxExecuteRows) {
                 $this->batchInsertDuplicate(Regions::tableName(), $regionColumns, $regionRows, $regionDuplicates)->execute();
                 $regionRows = [];
             }
         }
         if (count($regionRows) > 0) {
             $this->batchInsertDuplicate(Regions::tableName(), $regionColumns, $regionRows, $regionDuplicates)->execute();
         }
         fclose($handle);
     }
     return $return;
 }
 public function down()
 {
     $this->dropTable(Regions::tableName());
 }