Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand All @@ -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++;
Expand All @@ -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) {
Expand Down