Commit 4dab0562 authored by Simon Gadient's avatar Simon Gadient

[BUGFIX] Property mapping with same column

refs KIME-4583
parent bfb16fff
...@@ -30,7 +30,7 @@ class FrontendMappingUtility { ...@@ -30,7 +30,7 @@ class FrontendMappingUtility {
$domainMappingProperties = $spreadsheetImportService->getMappingProperties(); $domainMappingProperties = $spreadsheetImportService->getMappingProperties();
foreach ($domainMappingProperties as $property => $mapping) { foreach ($domainMappingProperties as $property => $mapping) {
$column = $request->getArgument($property); $column = $request->getArgument($property);
$mappings[$column] = $property; $mappings[$property] = $column;
} }
return $mappings; return $mappings;
} }
......
...@@ -35,6 +35,11 @@ class SpreadsheetImportService { ...@@ -35,6 +35,11 @@ class SpreadsheetImportService {
*/ */
protected $mappingProperties; protected $mappingProperties;
/**
* @var array
*/
protected $columnPropertyMapping;
/** /**
* @Flow\InjectConfiguration * @Flow\InjectConfiguration
* @var array * @var array
...@@ -74,6 +79,7 @@ class SpreadsheetImportService { ...@@ -74,6 +79,7 @@ class SpreadsheetImportService {
$this->spreadsheetImport = $spreadsheetImport; $this->spreadsheetImport = $spreadsheetImport;
$this->context = $this->settings[$spreadsheetImport->getContext()]; $this->context = $this->settings[$spreadsheetImport->getContext()];
$this->initDomainMappingProperties(); $this->initDomainMappingProperties();
$this->initColumnPropertyMapping();
return $this; return $this;
} }
...@@ -89,6 +95,16 @@ class SpreadsheetImportService { ...@@ -89,6 +95,16 @@ class SpreadsheetImportService {
} }
} }
/**
* Flip mapping and return it as a 2-dim array in case the same column is assigned to multiple properties
*/
private function initColumnPropertyMapping() {
$this->columnPropertyMapping = array();
foreach ($this->spreadsheetImport->getMapping() as $property => $column) {
$this->columnPropertyMapping[$column][] = $property;
}
}
/** /**
* Adds additional mapping properties to the domain mapping properties retrieved by annotations. This increases * Adds additional mapping properties to the domain mapping properties retrieved by annotations. This increases
* flexibility for dynamic property mapping. * flexibility for dynamic property mapping.
...@@ -109,7 +125,7 @@ class SpreadsheetImportService { ...@@ -109,7 +125,7 @@ class SpreadsheetImportService {
/** /**
* @return array * @return array
*/ */
public function getDomainMappingIdentifierProperties() { private function getDomainMappingIdentifierProperties() {
$domainMappingProperties = array(); $domainMappingProperties = array();
$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class); $properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class);
foreach ($properties as $property) { foreach ($properties as $property) {
...@@ -164,7 +180,6 @@ class SpreadsheetImportService { ...@@ -164,7 +180,6 @@ class SpreadsheetImportService {
* @return void * @return void
*/ */
public function import() { public function import() {
// TODO: This simply creates the objects for now without update or delete
$totalInserted = 0; $totalInserted = 0;
$totalUpdated = 0; $totalUpdated = 0;
$totalDeleted = 0; $totalDeleted = 0;
...@@ -233,7 +248,7 @@ class SpreadsheetImportService { ...@@ -233,7 +248,7 @@ class SpreadsheetImportService {
private function findObjectByIdentifierPropertiesPerRow(array $identifierProperties, \PHPExcel_Worksheet_Row $row) { private function findObjectByIdentifierPropertiesPerRow(array $identifierProperties, \PHPExcel_Worksheet_Row $row) {
$query = $this->getDomainRepository()->createQuery(); $query = $this->getDomainRepository()->createQuery();
$constraints = array(); $constraints = array();
$spreadsheetImportMapping = array_flip($this->spreadsheetImport->getMapping()); $spreadsheetImportMapping = $this->spreadsheetImport->getMapping();
/** @var Mapping $mapping */ /** @var Mapping $mapping */
foreach ($identifierProperties as $property => $mapping) { foreach ($identifierProperties as $property => $mapping) {
$column = $spreadsheetImportMapping[$property]; $column = $spreadsheetImportMapping[$property];
...@@ -266,18 +281,18 @@ class SpreadsheetImportService { ...@@ -266,18 +281,18 @@ class SpreadsheetImportService {
* @param \PHPExcel_Worksheet_Row $row * @param \PHPExcel_Worksheet_Row $row
*/ */
private function setObjectPropertiesByRow($newObject, $row) { private function setObjectPropertiesByRow($newObject, $row) {
// TODO: Cache $domainMappingProperties and $mappings $domainMappingProperties = $this->mappingProperties;
$domainMappingProperties = $this->getMappingProperties();
$mappings = $this->spreadsheetImport->getMapping();
/** @var \PHPExcel_Cell $cell */ /** @var \PHPExcel_Cell $cell */
foreach ($row->getCellIterator() as $cell) { foreach ($row->getCellIterator() as $cell) {
$column = $cell->getColumn(); $column = $cell->getColumn();
if (array_key_exists($column, $mappings)) { if (array_key_exists($column, $this->columnPropertyMapping)) {
$property = $mappings[$column]; $properties = $this->columnPropertyMapping[$column];
/** @var Mapping $mapping */ foreach ($properties as $property) {
$mapping = $domainMappingProperties[$property]; /** @var Mapping $mapping */
$setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter; $mapping = $domainMappingProperties[$property];
$newObject->$setter($cell->getValue()); $setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter;
$newObject->$setter($cell->getValue());
}
} }
} }
} }
......
...@@ -12,7 +12,6 @@ namespace WE\SpreadsheetImport\Tests\Functional; ...@@ -12,7 +12,6 @@ namespace WE\SpreadsheetImport\Tests\Functional;
* */ * */
use TYPO3\Flow\Reflection\ReflectionService; use TYPO3\Flow\Reflection\ReflectionService;
use TYPO3\Flow\Resource\Resource;
use TYPO3\Flow\Resource\ResourceManager; use TYPO3\Flow\Resource\ResourceManager;
use TYPO3\Flow\Tests\FunctionalTestCase; use TYPO3\Flow\Tests\FunctionalTestCase;
use WE\SpreadsheetImport\Annotations\Mapping; use WE\SpreadsheetImport\Annotations\Mapping;
...@@ -63,7 +62,7 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase { ...@@ -63,7 +62,7 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase {
$spreadsheetImport->setContext('testing'); $spreadsheetImport->setContext('testing');
$resource = $this->resourceManager->importResource(__DIR__ . '/Fixtures/Resources/sample.xlsx'); $resource = $this->resourceManager->importResource(__DIR__ . '/Fixtures/Resources/sample.xlsx');
$spreadsheetImport->setFile($resource); $spreadsheetImport->setFile($resource);
$spreadsheetImport->setMapping(array('C' => 'id', 'A' => 'name')); $spreadsheetImport->setMapping(array('id' => 'C', 'name' => 'A'));
$this->spreadsheetImportService->init($spreadsheetImport); $this->spreadsheetImportService->init($spreadsheetImport);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment