Example #1
0
 public function __construct($table, $upload_id)
 {
     parent::__construct($table);
     $replaces = array('/\\+/' => '-plus-', '/[^a-z0-9]+/' => '-');
     $this->upload_id = $upload_id;
     $this->patterns = array_keys($replaces);
     $this->replacements = array_values($replaces);
     $this->on('parsed', [$this, 'addUploadId']);
 }
Example #2
0
 public function import(Import $import, RowProvider $rowProvider)
 {
     if ($this->headers) {
         $headers = $rowProvider->nextRow();
     }
     $this->normalizer->setDb($this->db);
     $this->normalizer->cacheTables($import->relations);
     $this->db->transactionBegin();
     $numRows = 0;
     while ($row = $rowProvider->nextRow()) {
         $row = $import->doCallbacks('input', $row);
         if (empty($row)) {
             continue;
         }
         $main = array();
         foreach ($import->columns as $col) {
             if (!isset($col->position)) {
                 $main[$col->name] = null;
                 continue;
             }
             $pos = $col->position;
             if (!isset($row[$pos])) {
                 throw new ConfigException("Column position [{$col->position}] does not exist");
             }
             try {
                 $main[$col->name] = $this->parser->parseValue($col, $row[$pos]);
             } catch (ParseException $ex) {
                 \Log::error($ex);
                 $main[$col->name] = null;
                 if ($this->haltOnError) {
                     $this->db->transactionRollback();
                     throw $ex;
                 } else {
                     $this->errors[] = $ex->getMessage();
                 }
             }
         }
         $main = $import->doCallbacks('parsed', $main);
         if (empty($main)) {
             continue;
         }
         foreach ($import->relations as $rel) {
             $main = $this->normalizer->normalize($rel, $main);
         }
         $main = $import->doCallbacks('normalized', $main);
         if (empty($main)) {
             continue;
         }
         $insert = array();
         foreach ($import->columns as $col) {
             if (!$col->insert) {
                 continue;
             }
             $insert[$col->name] = $main[$col->name];
         }
         $this->db->bufferedCreate($import->table, $insert);
         $numRows++;
     }
     $this->db->flushBuffers();
     $import->beforeCommit();
     $this->db->transactionCommit();
     return $numRows;
 }