From 711890389c986128183d4b12bac2409bb900ffc7 Mon Sep 17 00:00:00 2001 From: Arunas Mazeika Date: Fri, 16 Sep 2022 15:03:03 +0200 Subject: [PATCH] #619 Convert empty CSV values --- .../migrator/behavior/import/insert.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/code/libraries/joomlatools-components/migrator/migrator/behavior/import/insert.php b/code/libraries/joomlatools-components/migrator/migrator/behavior/import/insert.php index fa65fd181..89d8407d9 100644 --- a/code/libraries/joomlatools-components/migrator/migrator/behavior/import/insert.php +++ b/code/libraries/joomlatools-components/migrator/migrator/behavior/import/insert.php @@ -123,6 +123,7 @@ protected function _insertCSV(SplFileObject $file, $table, $offset = 0, $limit = $rows_per_query = 20; $queue_count = 0; $total_count = 0; + $columns = array(); foreach ($file as $i => $row) { @@ -136,6 +137,15 @@ protected function _insertCSV(SplFileObject $file, $table, $offset = 0, $limit = $query->columns($row); + $schema = $this->getObject('lib:database.table.default', array('name' => $table))->getSchema(); + + foreach ($row as $column) + { + if (isset($schema->columns[$column])) { + $columns[] = $schema->columns[$column]; + } + } + continue; } @@ -147,6 +157,8 @@ protected function _insertCSV(SplFileObject $file, $table, $offset = 0, $limit = $this->_convertNullDates($row); + $this->_convertEmptyValues($row, $columns); + $query->values($row); $total_count++; $queue_count++; @@ -171,6 +183,25 @@ protected function _insertCSV(SplFileObject $file, $table, $offset = 0, $limit = return $total_count; } + protected function _convertEmptyValues(&$row, $columns) + { + $types = array('int', 'bigint', 'tinyint', 'mediumint', 'smallint', 'time', 'timestamp', 'year', 'date', 'datetime'); // A list of allowed types for the conversion + + foreach ($row as $key => $value) + { + if (isset($columns[$key])) + { + $default = $columns[$key]->default; + $required = $columns[$key]->required; + $type = $columns[$key]->type; + + if ($value === '' && $default === null && !$required && in_array($type, $types)) { + $row[$key] = null; + } + } + } + } + protected function _convertNullDates(&$row) { foreach ($row as $i => $value) {