mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
commit
53e3e6d2b4
@ -101,7 +101,7 @@ abstract class BulkLoader extends ViewableData {
|
||||
*
|
||||
* {@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
|
||||
|
@ -162,15 +162,12 @@ class CsvBulkLoader extends BulkLoader {
|
||||
*/
|
||||
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 @@ class CsvBulkLoader extends BulkLoader {
|
||||
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 @@ class CsvBulkLoaderTest extends SapphireTest {
|
||||
$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"
|
Can't render this file because it contains an unexpected character in line 1 and column 81.
|
Loading…
x
Reference in New Issue
Block a user