Skip to content

Commit 8497b6e

Browse files
author
Thomas Courthial
committed
[MULTIPLE KEY] Adding handling of items identified by multiple key +
using custom find function (#multipleKey)
1 parent 7333799 commit 8497b6e

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

Services/DataMapper/XmlMapper.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,41 @@ private function xmlElementToEntity(array $mapping, \SimpleXMLElement $xmlElemen
7474
$oldObject = null;
7575
$$objectName = null;
7676
if (isset($description['key'])) {
77-
// We attempt to retrieve an existing object corresponding to XML data
78-
if ((string)$description['key']['xmlKey'] == '@uniqueId') {
79-
$uniqueId = $xmlElement->xpath('@id');
80-
$nodeParent = $xmlElement->xpath('../..');
81-
$key = array(0 => $nodeParent[0]->getName().'-'.(string)$uniqueId[0]);
82-
// echo $nodeParent[0]->getName().'-'.(string)$uniqueId[0];
77+
// Multiple key
78+
if (is_array($description['key']['xmlKey'])) {
79+
$keys = array();
80+
foreach($description['key']['xmlKey'] as $id => $key) {
81+
$tmpKey = $xmlElement->xpath($key);
82+
$keys[$description['key']['keyName'][$id]] = $tmpKey[0];
83+
}
84+
} else {
85+
// We attempt to retrieve an existing object corresponding to XML data
86+
if ((string)$description['key']['xmlKey'] == '@uniqueId') {
87+
$uniqueId = $xmlElement->xpath('@id');
88+
$nodeParent = $xmlElement->xpath('../..');
89+
$keys = array(0 => $nodeParent[0]->getName().'-'.(string)$uniqueId[0]);
90+
}
91+
else {
92+
$keys = $xmlElement->xpath($description['key']['xmlKey']);
93+
}
8394
}
84-
else {
85-
$key = $xmlElement->xpath($description['key']['xmlKey']);
95+
96+
// If there is a findFunction, use it instead of findOneBy
97+
if(isset($description['key']['findFunction'])) {
98+
$findFunction = $description['key']['findFunction'];
99+
} else {
100+
$findFunction = 'findOneBy';
86101
}
102+
87103
// If xmlKey not found in XMl Data, there's no object to create. So we return null
88-
if (!isset($key[0])) {
104+
if (!isset($description['key']['xmlKey'])) {
89105
return null;
106+
} elseif(is_array($description['key']['xmlKey'])) {
107+
$oldObject = $em->getRepository($description['repository'])->$findFunction($keys);
108+
} else{
109+
$oldObject = $em->getRepository($description['repository'])->$findFunction(array($description['key']['keyName'] => $keys[0]));
90110
}
91-
$oldObject = $em->getRepository($description['repository'])->findOneBy(array($description['key']['keyName'] => $key[0]));
111+
92112
}
93113

94114
if (!is_null($oldObject)) {

0 commit comments

Comments
 (0)