mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: added option to truncate (clear) database table before importing a new CSV file with CSVBulkerLoader and ModelAdmin.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@85709 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
ebce107d07
commit
2cf002e333
@ -124,15 +124,24 @@ abstract class BulkLoader extends ViewableData {
|
||||
}
|
||||
|
||||
/*
|
||||
* Load the given file via {@link self::processAll()} and {@link self::processRecord()}.
|
||||
* Load the given file via {@link self::processAll()} and {@link self::processRecord()}. Optionally truncates (clear) the table before it imports.
|
||||
*
|
||||
* @return BulkLoader_Result See {@link self::processAll()}
|
||||
*/
|
||||
public function load($filepath, $memory_limit='512M') {
|
||||
public function load($filepath, $memory_limit='512M', $clear_table_before_import=false) {
|
||||
ini_set('max_execution_time', 3600);
|
||||
|
||||
increase_memory_limit_to($memory_limit);
|
||||
|
||||
if ($clear_table_before_import) {
|
||||
$objectSet = DataObject::get($this->objectClass); //get all instances of the to be imported data object
|
||||
if (!empty($objectSet)) {
|
||||
foreach($objectSet as $obj) {
|
||||
$obj->delete(); //deleting objects ensures that versions are also deleted (truncating would just delete the main table); performance is slower, however
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->processAll($filepath);
|
||||
}
|
||||
|
||||
|
@ -32,6 +32,21 @@ class CsvBulkLoaderTest extends SapphireTest {
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test plain import with clear_table_before_import
|
||||
*/
|
||||
function testClearTableBeforeImport() {
|
||||
$loader = new CsvBulkLoader('CsvBulkLoaderTest_Player');
|
||||
$filepath = Director::baseFolder() . '/sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv';
|
||||
$results1 = $loader->load($filepath, '512MB', false); //leave existing data there on first CSV import
|
||||
$this->assertEquals(4, $results1->Count(), 'Test correct count of imported data on first load');
|
||||
|
||||
$results2 = $loader->load($filepath, '512MB', true); //delete existing data before doing second CSV import
|
||||
$resultDataObject = DataObject::get('CsvBulkLoaderTest_Player'); //get all instances of the loaded DataObject from the database and count them
|
||||
|
||||
$this->assertEquals(4, $resultDataObject->Count(), 'Test if existing data is deleted before new data is added');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test import with manual column mapping
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user