Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Event/PostImportRowEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(
private readonly int $insertId,
private readonly array $csvRecord,
private readonly ImportFromCsv $importInstance,
private readonly array $rawCsvLine = [],
) {
}

Expand All @@ -48,6 +49,15 @@ public function getLineAsArray(): array
return $this->csvRecord;
}

/**
* Returns the unprocessed CSV line with all columns, including those
* that are not selected for import (values are neither trimmed nor mapped).
*/
public function getRawLineAsArray(): array
{
return $this->rawCsvLine;
}

public function getImportInstance(): ImportFromCsv
{
return $this->importInstance;
Expand Down
10 changes: 10 additions & 0 deletions src/Event/PreImportRowEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(
private array $dataRecord,
private readonly array $csvRecord,
private readonly ImportFromCsv $importInstance,
private readonly array $rawCsvLine = [],
) {
}

Expand All @@ -47,6 +48,15 @@ public function getLineAsArray(): array
return $this->csvRecord;
}

/**
* Returns the unprocessed CSV line with all columns, including those
* that are not selected for import (values are neither trimmed nor mapped).
*/
public function getRawLineAsArray(): array
{
return $this->rawCsvLine;
}

public function getImportInstance(): ImportFromCsv
{
return $this->importInstance;
Expand Down
25 changes: 23 additions & 2 deletions src/Import/ImportFromCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public function importCsv(File $csvFile, string $tableName, string $importMode,
$arrReportValues = [];
$doNotSave = false;

// Keep the CSV data of each line, keyed by the same line number as $importData,
// so that the row events receive the data of the row being imported.
$csvRecordsPerLine = [];
$rawCsvLinesPerLine = [];

// Process each line
foreach ($csvLines as $csvLine) {
$csvRecord = [];
Expand Down Expand Up @@ -231,6 +236,9 @@ public function importCsv(File $csvFile, string $tableName, string $importMode,
// Update current line (CSV)
++$this->currentLine;

$csvRecordsPerLine[$this->currentLine] = $csvRecord;
$rawCsvLinesPerLine[$this->currentLine] = $csvLine;

// Update processed rows counter
++$this->countProcessedRows;

Expand Down Expand Up @@ -343,7 +351,13 @@ public function importCsv(File $csvFile, string $tableName, string $importMode,
$this->connection->beginTransaction();

try {
$event = new PreImportRowEvent($this->config->tableName, $set, $csvRecord, $this);
$event = new PreImportRowEvent(
$this->config->tableName,
$set,
$csvRecordsPerLine[$currentLine],
$this,
$rawCsvLinesPerLine[$currentLine],
);
$this->eventDispatcher->dispatch($event);

$id = $this->importPersister->upsert(
Expand All @@ -358,7 +372,14 @@ public function importCsv(File $csvFile, string $tableName, string $importMode,
$this->connection->rollBack();
}

$event = new PostImportRowEvent($this->config->tableName, $set, $id, $csvRecord, $this);
$event = new PostImportRowEvent(
$this->config->tableName,
$set,
$id,
$csvRecordsPerLine[$currentLine],
$this,
$rawCsvLinesPerLine[$currentLine],
);
$this->eventDispatcher->dispatch($event);
} catch (\Throwable $e) {
$doNotSave = true;
Expand Down
Loading