From 83b1fec52f2303dc13a9de7083e48d8e3c9cc0be Mon Sep 17 00:00:00 2001 From: Marko Cupic <1525166+markocupic@users.noreply.github.com> Date: Mon, 12 Oct 2026 13:51:31 +0200 Subject: [PATCH] Fix row-event-csv-record --- src/Event/PostImportRowEvent.php | 10 ++++++++++ src/Event/PreImportRowEvent.php | 10 ++++++++++ src/Import/ImportFromCsv.php | 25 +++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/Event/PostImportRowEvent.php b/src/Event/PostImportRowEvent.php index f98ee21..7b07ed3 100644 --- a/src/Event/PostImportRowEvent.php +++ b/src/Event/PostImportRowEvent.php @@ -25,6 +25,7 @@ public function __construct( private readonly int $insertId, private readonly array $csvRecord, private readonly ImportFromCsv $importInstance, + private readonly array $rawCsvLine = [], ) { } @@ -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; diff --git a/src/Event/PreImportRowEvent.php b/src/Event/PreImportRowEvent.php index d88de6d..7bbbf9f 100644 --- a/src/Event/PreImportRowEvent.php +++ b/src/Event/PreImportRowEvent.php @@ -24,6 +24,7 @@ public function __construct( private array $dataRecord, private readonly array $csvRecord, private readonly ImportFromCsv $importInstance, + private readonly array $rawCsvLine = [], ) { } @@ -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; diff --git a/src/Import/ImportFromCsv.php b/src/Import/ImportFromCsv.php index ae4a1d0..49e6528 100644 --- a/src/Import/ImportFromCsv.php +++ b/src/Import/ImportFromCsv.php @@ -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 = []; @@ -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; @@ -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( @@ -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;