Merge pull request #1781 from jedateach/csvloader-duplicatecheck-fix

FIX: CSVBulkLoader duplicate check can now check more than one field
This commit is contained in:
Ingo Schommer 2013-04-21 12:45:06 -07:00
commit 53e3e6d2b4
4 changed files with 15 additions and 15 deletions

View File

@ -101,7 +101,7 @@ abstract class BulkLoader extends ViewableData {
* *
* {@see Member::$unique_identifier_field}. * {@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> * <code>
* <?php * <?php

View File

@ -162,15 +162,12 @@ class CsvBulkLoader extends BulkLoader {
*/ */
public function findExistingObject($record) { public function findExistingObject($record) {
$SNG_objectClass = singleton($this->objectClass); $SNG_objectClass = singleton($this->objectClass);
// checking for existing records (only if not already found) // checking for existing records (only if not already found)
foreach($this->duplicateChecks as $fieldName => $duplicateCheck) { foreach($this->duplicateChecks as $fieldName => $duplicateCheck) {
if(is_string($duplicateCheck)) { if(is_string($duplicateCheck)) {
$SQL_fieldName = Convert::raw2sql($duplicateCheck); $SQL_fieldName = Convert::raw2sql($duplicateCheck);
if(!isset($record[$fieldName])) { if(!isset($record[$fieldName]) || empty($record[$fieldName])) { //skip current duplicate check if field value is empty
return false; continue;
//user_error("CsvBulkLoader:processRecord: Couldn't find duplicate identifier '{$fieldName}'
//in columns", E_USER_ERROR);
} }
$SQL_fieldValue = Convert::raw2sql($record[$fieldName]); $SQL_fieldValue = Convert::raw2sql($record[$fieldName]);
$existingRecord = DataObject::get_one($this->objectClass, "\"$SQL_fieldName\" = '{$SQL_fieldValue}'"); $existingRecord = DataObject::get_one($this->objectClass, "\"$SQL_fieldName\" = '{$SQL_fieldValue}'");
@ -184,17 +181,16 @@ class CsvBulkLoader extends BulkLoader {
user_error("CsvBulkLoader::processRecord():" user_error("CsvBulkLoader::processRecord():"
. " {$duplicateCheck['callback']} not found on importer or object class.", E_USER_ERROR); . " {$duplicateCheck['callback']} not found on importer or object class.", E_USER_ERROR);
} }
if($existingRecord) {
if($existingRecord) return $existingRecord; return $existingRecord;
}
} else { } else {
user_error('CsvBulkLoader::processRecord(): Wrong format for $duplicateChecks', E_USER_ERROR); user_error('CsvBulkLoader::processRecord(): Wrong format for $duplicateChecks', E_USER_ERROR);
} }
} }
return false; return false;
} }
/** /**
* Determine wether any loaded files should be parsed * Determine wether any loaded files should be parsed
* with a header-row (otherwise we rely on {@link self::$columnMap}. * with a header-row (otherwise we rely on {@link self::$columnMap}.

View File

@ -151,7 +151,10 @@ class CsvBulkLoaderTest extends SapphireTest {
$loader = new CsvBulkLoader('CsvBulkLoaderTest_Player'); $loader = new CsvBulkLoader('CsvBulkLoaderTest_Player');
$filepath = $this->getCurrentAbsolutePath() . '/CsvBulkLoaderTest_PlayersWithId.csv'; $filepath = $this->getCurrentAbsolutePath() . '/CsvBulkLoaderTest_PlayersWithId.csv';
$loader->duplicateChecks = array( $loader->duplicateChecks = array(
'ExternalIdentifier' => 'ExternalIdentifier' 'ExternalIdentifier' => 'ExternalIdentifier',
'NonExistantIdentifier' => 'ExternalIdentifier',
'ExternalIdentifier' => 'ExternalIdentifier',
'AdditionalIdentifier' => 'ExternalIdentifier'
); );
$results = $loader->load($filepath); $results = $loader->load($filepath);
$createdPlayers = $results->Created(); $createdPlayers = $results->Created();

View File

@ -1,4 +1,5 @@
"ExternalIdentifier","FirstName","Biography","Birthday" "ExternalIdentifier","FirstName","Biography","Birthday"," AdditionalIdentifier "
222b,"John","","31/01/1988" 222b,"John","","31/01/1988",""
222b,"John","He's a good guy","" 222b,"John","He's a good guy","",""
9000a,"Jamie","Pretty old\, with an escaped comma","31/01/1882" 9000a,"Jamie","Pretty old\, with an escaped comma","31/01/1882",""
,"Fiona","A wise woman","","sql'unsafeid"
Can't render this file because it contains an unexpected character in line 1 and column 81.