Permalink
Browse files

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.
  • Loading branch information...
1 parent d07fb43 commit c4eac5310e1f4070ad05acf6dc0200e9fc025636 @jedateach jedateach committed Apr 19, 2013
Showing with 15 additions and 15 deletions.
  1. +1 −1 dev/BulkLoader.php
  2. +5 −9 dev/CsvBulkLoader.php
  3. +4 −1 tests/dev/CsvBulkLoaderTest.php
  4. +5 −4 tests/dev/CsvBulkLoaderTest_PlayersWithId.csv
View
@@ -101,7 +101,7 @@
*
* {@see Member::$unique_identifier_field}.
*
- * If multiple checks are specified, the first one "wins".
+ * If multiple checks are specified, the first non-empty field "wins".
*
* <code>
* <?php
View
@@ -162,15 +162,12 @@ protected function processRecord($record, $columnMap, &$results, $preview = fals
*/
public function findExistingObject($record) {
$SNG_objectClass = singleton($this->objectClass);
-
// checking for existing records (only if not already found)
foreach($this->duplicateChecks as $fieldName => $duplicateCheck) {
if(is_string($duplicateCheck)) {
$SQL_fieldName = Convert::raw2sql($duplicateCheck);
- if(!isset($record[$fieldName])) {
- return false;
- //user_error("CsvBulkLoader:processRecord: Couldn't find duplicate identifier '{$fieldName}'
- //in columns", E_USER_ERROR);
+ if(!isset($record[$fieldName]) || empty($record[$fieldName])) { //skip current duplicate check if field value is empty
+ continue;
}
$SQL_fieldValue = Convert::raw2sql($record[$fieldName]);
$existingRecord = DataObject::get_one($this->objectClass, "\"$SQL_fieldName\" = '{$SQL_fieldValue}'");
@@ -184,17 +181,16 @@ public function findExistingObject($record) {
user_error("CsvBulkLoader::processRecord():"
. " {$duplicateCheck['callback']} not found on importer or object class.", E_USER_ERROR);
}
-
- if($existingRecord) return $existingRecord;
+ if($existingRecord) {
+ return $existingRecord;
+ }
} else {
user_error('CsvBulkLoader::processRecord(): Wrong format for $duplicateChecks', E_USER_ERROR);
}
}
-
return false;
}
-
/**
* Determine wether any loaded files should be parsed
* with a header-row (otherwise we rely on {@link self::$columnMap}.
@@ -151,7 +151,10 @@ public function testLoadWithIdentifiers() {
$loader = new CsvBulkLoader('CsvBulkLoaderTest_Player');
$filepath = $this->getCurrentAbsolutePath() . '/CsvBulkLoaderTest_PlayersWithId.csv';
$loader->duplicateChecks = array(
- 'ExternalIdentifier' => 'ExternalIdentifier'
+ 'ExternalIdentifier' => 'ExternalIdentifier',
+ 'NonExistantIdentifier' => 'ExternalIdentifier',
+ 'ExternalIdentifier' => 'ExternalIdentifier',
+ 'AdditionalIdentifier' => 'ExternalIdentifier'
);
$results = $loader->load($filepath);
$createdPlayers = $results->Created();
@@ -1,4 +1,5 @@
-"ExternalIdentifier","FirstName","Biography","Birthday"
-222b,"John","","31/01/1988"
-222b,"John","He's a good guy",""
-9000a,"Jamie","Pretty old\, with an escaped comma","31/01/1882"
+"ExternalIdentifier","FirstName","Biography","Birthday"," AdditionalIdentifier "
+222b,"John","","31/01/1988",""
+222b,"John","He's a good guy","",""
+9000a,"Jamie","Pretty old\, with an escaped comma","31/01/1882",""
+,"Fiona","A wise woman","","sql'unsafeid"

0 comments on commit c4eac53

Please sign in to comment.