Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
WE.SpreadsheetImport
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Labels
Merge Requests
0
Merge Requests
0
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
packages
WE.SpreadsheetImport
Commits
4dab0562
Commit
4dab0562
authored
Oct 26, 2016
by
Simon Gadient
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[BUGFIX] Property mapping with same column
refs KIME-4583
parent
bfb16fff
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
15 deletions
+29
-15
Classes/WE/SpreadsheetImport/FrontendMappingUtility.php
Classes/WE/SpreadsheetImport/FrontendMappingUtility.php
+1
-1
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php
+27
-12
Tests/Functional/SpreadsheetImportServiceTest.php
Tests/Functional/SpreadsheetImportServiceTest.php
+1
-2
No files found.
Classes/WE/SpreadsheetImport/FrontendMappingUtility.php
View file @
4dab0562
...
@@ -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
;
}
}
...
...
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php
View file @
4dab0562
...
@@ -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
*/
*/
p
ublic
function
getDomainMappingIdentifierProperties
()
{
p
rivate
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,14 +281,13 @@ class SpreadsheetImportService {
...
@@ -266,14 +281,13 @@ 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
];
foreach
(
$properties
as
$property
)
{
/** @var Mapping $mapping */
/** @var Mapping $mapping */
$mapping
=
$domainMappingProperties
[
$property
];
$mapping
=
$domainMappingProperties
[
$property
];
$setter
=
empty
(
$mapping
->
setter
)
?
'set'
.
ucfirst
(
$property
)
:
$mapping
->
setter
;
$setter
=
empty
(
$mapping
->
setter
)
?
'set'
.
ucfirst
(
$property
)
:
$mapping
->
setter
;
...
@@ -281,4 +295,5 @@ class SpreadsheetImportService {
...
@@ -281,4 +295,5 @@ class SpreadsheetImportService {
}
}
}
}
}
}
}
}
}
Tests/Functional/SpreadsheetImportServiceTest.php
View file @
4dab0562
...
@@ -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
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment