Skip to content

Commit 5248f9f

Browse files
committed
correct and dynamic id mapping
1 parent 7cf2c88 commit 5248f9f

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

src/administrator/components/com_ccm/src/Helper/MigrationHelper.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,20 @@ public static function isSupportedFileType($sourceUrl) {
168168
* @param mixed $migrationMap The migration map
169169
* @return mixed The mapped ID or original value if no mapping found
170170
*/
171-
public static function mapEntityId($value, $migrationMap) {
171+
public static function mapEntityId($value, $migrationMap, $entityType = null) {
172172
if (empty($value)) {
173173
return $value;
174174
}
175175

176-
foreach ($migrationMap as $entityType => $mappings) {
177-
// Check for ID mapping
178-
if (isset($mappings['ids']) && isset($mappings['ids'][$value])) {
179-
error_log("[MigrationHelper] 🔗 Mapping ID '$value' using entity type '$entityType' → '{$mappings['ids'][$value]}'");
176+
if ($entityType && isset($migrationMap[$entityType]['ids'][$value])) {
177+
return $migrationMap[$entityType]['ids'][$value];
178+
}
179+
// fallback: search all types (old behavior, optional)
180+
foreach ($migrationMap as $type => $mappings) {
181+
if (isset($mappings['ids'][$value])) {
180182
return $mappings['ids'][$value];
181183
}
182184
}
183-
184-
return $value; // Return original value if no mapping found
185+
return $value;
185186
}
186187
}

src/administrator/components/com_ccm/src/Model/MigrationModel.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,11 @@ private function convertCcmToTargetCms($ccmItems, $targetCms, $targetType) {
290290
$targetItem = [];
291291
foreach ($targetToCcm as $targetKey => $ccmMap) {
292292
if (is_array($ccmMap)) {
293-
$ccmKey = $ccmMap['ccm'] ?? null;
294-
$type = $ccmMap['type'] ?? null;
295-
$format = $ccmMap['format'] ?? null;
296-
$value = null;
293+
$ccmKey = $ccmMap['ccm'] ?? null;
294+
$type = $ccmMap['type'] ?? null;
295+
$format = $ccmMap['format'] ?? null;
296+
$entityType = $ccmMap['entityType'] ?? null;
297+
$value = null;
297298

298299
if ($ccmKey && isset($ccmItem[$ccmKey])) {
299300
$value = $ccmItem[$ccmKey];
@@ -450,21 +451,21 @@ private function convertCcmToTargetCms($ccmItems, $targetCms, $targetType) {
450451
foreach ($value as $arrayValue) {
451452
if (is_numeric($arrayValue) || (is_string($arrayValue) && ctype_digit(trim($arrayValue)))) {
452453
$numericValue = intval($arrayValue);
453-
$mappedValue = MigrationHelper::mapEntityId($numericValue, $this->migrationMap);
454+
$mappedValue = MigrationHelper::mapEntityId($numericValue, $this->migrationMap, $entityType);
454455
$mappedValues[] = $mappedValue;
455456
}
456457
}
457458
$value = $mappedValues;
458459
}
459460
} elseif (is_numeric($value) || (is_string($value) && ctype_digit(trim($value)))) {
460-
$mappedValue = MigrationHelper::mapEntityId(intval($value), $this->migrationMap);
461+
$mappedValue = MigrationHelper::mapEntityId(intval($value), $this->migrationMap, $entityType);
461462
$value = [$mappedValue];
462463
} elseif (is_string($value) && strpos($value, ',') !== false) {
463464
$ids = array_map('trim', explode(',', $value));
464465
$mappedValues = [];
465466
foreach ($ids as $id) {
466467
if (is_numeric($id) || ctype_digit($id)) {
467-
$mappedValue = MigrationHelper::mapEntityId(intval($id), $this->migrationMap);
468+
$mappedValue = MigrationHelper::mapEntityId(intval($id), $this->migrationMap, $entityType);
468469
$mappedValues[] = $mappedValue;
469470
}
470471
}
@@ -519,13 +520,13 @@ private function convertCcmToTargetCms($ccmItems, $targetCms, $targetType) {
519520
$value = $value->ID;
520521
error_log("[MigrationModel] Extracted ID from object for id_map: " . $value);
521522
}
522-
$value = MigrationHelper::mapEntityId($value, $this->migrationMap);
523+
$value = MigrationHelper::mapEntityId($value, $this->migrationMap, $entityType);
523524
}
524525
break;
525526
}
526527
} else {
527528
if (($type === 'string' || $type === 'integer') && !empty($value)) {
528-
$value = MigrationHelper::mapEntityId($value, $this->migrationMap);
529+
$value = MigrationHelper::mapEntityId($value, $this->migrationMap, $entityType);
529530
}
530531
}
531532

src/administrator/components/com_ccm/src/Schema/joomla-ccm.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"created_by": {
3939
"ccm": "author",
4040
"type": "string",
41-
"format": "id_map"
41+
"format": "id_map",
42+
"entityType": "users"
4243
},
4344
"created_by_alias": null,
4445
"modified": {
@@ -75,7 +76,8 @@
7576
"ccm": "categories",
7677
"default": 2,
7778
"type": "string",
78-
"format": "id_map"
79+
"format": "id_map",
80+
"entityType": "categories"
7981
},
8082
"tags": {
8183
"ccm": "tags",

0 commit comments

Comments
 (0)