mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-04 07:08:34 +02:00
Merge pull request #1315 from dhensby/patch-4
FIX Adding preview method to CsvBulkLoader
This commit is contained in:
commit
f7400198d7
@ -32,6 +32,13 @@ class CsvBulkLoader extends BulkLoader {
|
|||||||
*/
|
*/
|
||||||
public $hasHeaderRow = true;
|
public $hasHeaderRow = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function preview($filepath) {
|
||||||
|
return $this->processAll($filepath, true);
|
||||||
|
}
|
||||||
|
|
||||||
protected function processAll($filepath, $preview = false) {
|
protected function processAll($filepath, $preview = false) {
|
||||||
$results = new BulkLoader_Result();
|
$results = new BulkLoader_Result();
|
||||||
|
|
||||||
@ -81,27 +88,36 @@ class CsvBulkLoader extends BulkLoader {
|
|||||||
if(!$relationObj || !$relationObj->exists()) {
|
if(!$relationObj || !$relationObj->exists()) {
|
||||||
$relationClass = $obj->has_one($relationName);
|
$relationClass = $obj->has_one($relationName);
|
||||||
$relationObj = new $relationClass();
|
$relationObj = new $relationClass();
|
||||||
$relationObj->write();
|
//write if we aren't previewing
|
||||||
|
if (!$preview) $relationObj->write();
|
||||||
}
|
}
|
||||||
$obj->{"{$relationName}ID"} = $relationObj->ID;
|
$obj->{"{$relationName}ID"} = $relationObj->ID;
|
||||||
|
//write if we are not previewing
|
||||||
|
if (!$preview) {
|
||||||
$obj->write();
|
$obj->write();
|
||||||
$obj->flushCache(); // avoid relation caching confusion
|
$obj->flushCache(); // avoid relation caching confusion
|
||||||
|
}
|
||||||
|
|
||||||
} elseif(strpos($fieldName, '.') !== false) {
|
} elseif(strpos($fieldName, '.') !== false) {
|
||||||
// we have a relation column with dot notation
|
// 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)
|
// always gives us an component (either empty or existing)
|
||||||
$relationObj = $obj->getComponent($relationName);
|
$relationObj = $obj->getComponent($relationName);
|
||||||
$relationObj->write();
|
if (!$preview) $relationObj->write();
|
||||||
$obj->{"{$relationName}ID"} = $relationObj->ID;
|
$obj->{"{$relationName}ID"} = $relationObj->ID;
|
||||||
|
//write if we are not previewing
|
||||||
|
if (!$preview) {
|
||||||
$obj->write();
|
$obj->write();
|
||||||
$obj->flushCache(); // avoid relation caching confusion
|
$obj->flushCache(); // avoid relation caching confusion
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// second run: save data
|
// second run: save data
|
||||||
foreach($record as $fieldName => $val) {
|
foreach($record as $fieldName => $val) {
|
||||||
|
//break out of the loop if we are previewing
|
||||||
|
if ($preview) break;
|
||||||
if($this->isNullValue($val, $fieldName)) continue;
|
if($this->isNullValue($val, $fieldName)) continue;
|
||||||
if(strpos($fieldName, '->') !== FALSE) {
|
if(strpos($fieldName, '->') !== FALSE) {
|
||||||
$funcName = substr($fieldName, 2);
|
$funcName = substr($fieldName, 2);
|
||||||
|
Loading…
Reference in New Issue
Block a user