Skip to content

Instantly share code, notes, and snippets.

@chillu
Created May 9, 2013 21:06
Show Gist options
  • Save chillu/5550629 to your computer and use it in GitHub Desktop.
Save chillu/5550629 to your computer and use it in GitHub Desktop.
commit d47b2026971d74cb9604a27edb6aa905499f132f
Author: Jeremy Shipman <jeremy@burnbright.net>
Date: Fri Apr 19 15:45:43 2013 +1200
Restored c4eac5310e1f (merge error)
FIX: Instead of CsvBulkLoader->findExistingRecord out right failing (i.e. no duplicate found) when the duplicate check field is empty, it will now continue on to check other duplicateCheck fields.
Added extra testing data to CSVBulkLoaderTest so that it fails.
diff --git a/dev/CsvBulkLoader.php b/dev/CsvBulkLoader.php
index c503f4a..d6ba98b 100644
--- a/dev/CsvBulkLoader.php
+++ b/dev/CsvBulkLoader.php
@@ -94,22 +94,22 @@ class CsvBulkLoader extends BulkLoader {
$obj->{"{$relationName}ID"} = $relationObj->ID;
//write if we are not previewing
if (!$preview) {
- $obj->write();
- $obj->flushCache(); // avoid relation caching confusion
+ $obj->write();
+ $obj->flushCache(); // avoid relation caching confusion
}
} elseif(strpos($fieldName, '.') !== false) {
// we have a relation column with dot notation
- list($relationName,$columnName) = explode('.', $fieldName);
+ list($relationName, $columnName) = explode('.', $fieldName);
// always gives us an component (either empty or existing)
$relationObj = $obj->getComponent($relationName);
if (!$preview) $relationObj->write();
$obj->{"{$relationName}ID"} = $relationObj->ID;
//write if we are not previewing
if (!$preview) {
- $obj->write();
- $obj->flushCache(); // avoid relation caching confusion
- }
+ $obj->write();
+ $obj->flushCache(); // avoid relation caching confusion
+ }
}
}
@@ -166,10 +166,8 @@ class CsvBulkLoader extends BulkLoader {
foreach($this->duplicateChecks as $fieldName => $duplicateCheck) {
if(is_string($duplicateCheck)) {
$SQL_fieldName = Convert::raw2sql($duplicateCheck);
- if(!isset($record[$SQL_fieldName])) {
- return false;
- //user_error("CsvBulkLoader:processRecord: Couldn't find duplicate identifier '{$fieldName}'
- //in columns", E_USER_ERROR);
+ if(!isset($record[$SQL_fieldName]) || empty($record[$SQL_fieldName])) { //skip current duplicate check if field value is empty
+ continue;
}
$SQL_fieldValue = Convert::raw2sql($record[$SQL_fieldName]);
$existingRecord = DataObject::get_one($this->objectClass, "\"$SQL_fieldName\" = '{$SQL_fieldValue}'");
diff --git a/tests/dev/CsvBulkLoaderTest.php b/tests/dev/CsvBulkLoaderTest.php
index 53cacbd..d1df150 100644
--- a/tests/dev/CsvBulkLoaderTest.php
+++ b/tests/dev/CsvBulkLoaderTest.php
@@ -152,7 +152,9 @@ class CsvBulkLoaderTest extends SapphireTest {
$filepath = $this->getCurrentAbsolutePath() . '/CsvBulkLoaderTest_PlayersWithId.csv';
$loader->duplicateChecks = array(
'ExternalIdentifier' => 'ExternalIdentifier',
+ 'NonExistantIdentifier' => 'ExternalIdentifier',
'ExternalIdentifier' => 'ExternalIdentifier',
+ 'AdditionalIdentifier' => 'ExternalIdentifier'
);
$results = $loader->load($filepath);
$createdPlayers = $results->Created();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment