public static function loadFromFile(RoyaltyPaymentFile $rpf) { $firstLine = CsvFileService::getFileFirstLine($rpf->path); $header = explode(',', strtolower(trim($firstLine))); $fieldsToFileHeaderPos = RoyaltyPayment::fieldsToFileHeaderPositions(); if (count($header) == count(RoyaltyPayment::fieldsToFileHeaderPositionsOld())) { $fieldsToFileHeaderPos = RoyaltyPayment::fieldsToFileHeaderPositionsOld(); } $query = "LOAD DATA LOCAL INFILE " . DB::connection()->getPdo()->quote($rpf->path) . "\n INTO TABLE " . RoyaltyPayment::table() . "\n CHARACTER SET 'utf8'\n FIELDS TERMINATED BY ','\n ENCLOSED BY '\"' " . RoyaltyPayment::detectNewLineChar($firstLine) . "\n IGNORE 1 LINES\n (@col" . implode(",@col", array_keys($header)) . ")\n SET "; foreach ($fieldsToFileHeaderPos as $position => $field) { if (array_key_exists($field, self::fieldFilters())) { $query .= " {$field}=" . self::fieldFilter($field, "@col" . ($position + 2)) . ", "; } else { $query .= " {$field}=TRIM(@col" . ($position + 2) . "), "; } } $query = rtrim($query, ", "); $query .= ", created_at = now(), updated_at = now(), company_id = " . $rpf->company_id . ", royalty_payment_file_id = " . $rpf->id; DB::connection()->getpdo()->exec($query); }