fixed import
This commit is contained in:
@@ -258,7 +258,17 @@ protected function simulateRow(Import $import, array $mapped, array $raw, array
|
||||
|
||||
// Resolve existing entity (uses EntityResolutionService internally)
|
||||
// Pass accumulated entityResults as context for chain resolution
|
||||
$existingEntity = $handler->resolve($entityData, array_merge($context, $entityResults));
|
||||
try {
|
||||
$existingEntity = $handler->resolve($entityData, array_merge($context, $entityResults));
|
||||
} catch (\Throwable $resolutionError) {
|
||||
// In simulation mode, resolution may fail due to simulated entities
|
||||
// Just treat as new entity
|
||||
\Log::debug("ImportSimulation: Resolution failed (treating as new)", [
|
||||
'entity' => $root,
|
||||
'error' => $resolutionError->getMessage(),
|
||||
]);
|
||||
$existingEntity = null;
|
||||
}
|
||||
|
||||
if ($existingEntity) {
|
||||
// Would update existing
|
||||
@@ -284,9 +294,14 @@ protected function simulateRow(Import $import, array $mapped, array $raw, array
|
||||
];
|
||||
|
||||
// Simulate entity creation for context (no actual ID)
|
||||
// Mark as simulated so resolution service knows not to use model methods
|
||||
$simulatedEntity = (object) $entityData;
|
||||
$simulatedEntity->_simulated = true;
|
||||
|
||||
$entityResults[$root] = [
|
||||
'entity' => (object) $entityData,
|
||||
'entity' => $simulatedEntity,
|
||||
'action' => 'inserted',
|
||||
'_simulated' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -420,23 +435,35 @@ protected function groupMappedDataByEntity(array $mapped): array
|
||||
|
||||
// Handle array values
|
||||
if (is_array($value)) {
|
||||
if (!isset($grouped[$entity])) {
|
||||
$grouped[$entity] = [];
|
||||
}
|
||||
|
||||
// Special case: activity.note should be kept as array in single instance
|
||||
if ($entity === 'activity' || $entity === 'activities') {
|
||||
if (!isset($grouped[$entity][0])) {
|
||||
$grouped[$entity][0] = [];
|
||||
if (!isset($grouped[$entity])) {
|
||||
$grouped[$entity] = [];
|
||||
}
|
||||
$grouped[$entity][0][$field] = $value; // Keep as array
|
||||
$grouped[$entity][$field] = $value; // Keep as array
|
||||
} else {
|
||||
// Create separate entity instances for each array value
|
||||
foreach ($value as $idx => $val) {
|
||||
if (!isset($grouped[$entity][$idx])) {
|
||||
$grouped[$entity][$idx] = [];
|
||||
// For other entities, only create multiple instances if:
|
||||
// 1. Entity doesn't exist yet, OR
|
||||
// 2. Entity has no other fields yet (is empty array)
|
||||
if (!isset($grouped[$entity])) {
|
||||
$grouped[$entity] = [];
|
||||
}
|
||||
|
||||
// If entity already has string-keyed fields, just set the array as field value
|
||||
// Otherwise, create separate instances
|
||||
$hasStringKeys = !empty($grouped[$entity]) && isset(array_keys($grouped[$entity])[0]) && is_string(array_keys($grouped[$entity])[0]);
|
||||
|
||||
if ($hasStringKeys) {
|
||||
// Entity has fields already - don't split, keep array as-is
|
||||
$grouped[$entity][$field] = $value;
|
||||
} else {
|
||||
// Create separate entity instances for each array value
|
||||
foreach ($value as $idx => $val) {
|
||||
if (!isset($grouped[$entity][$idx])) {
|
||||
$grouped[$entity][$idx] = [];
|
||||
}
|
||||
$grouped[$entity][$idx][$field] = $val;
|
||||
}
|
||||
$grouped[$entity][$idx][$field] = $val;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -445,14 +472,17 @@ protected function groupMappedDataByEntity(array $mapped): array
|
||||
$grouped[$entity] = [];
|
||||
}
|
||||
|
||||
// If entity is already an array (from previous grouped field), add to all instances
|
||||
if (isset($grouped[$entity][0]) && is_array($grouped[$entity][0])) {
|
||||
// Check if entity is already an array of instances (from previous grouped field)
|
||||
if (!empty($grouped[$entity]) && is_int(array_key_first($grouped[$entity]))) {
|
||||
// Entity has multiple instances - add field to all instances
|
||||
foreach ($grouped[$entity] as &$instance) {
|
||||
$instance[$field] = $value;
|
||||
if (is_array($instance)) {
|
||||
$instance[$field] = $value;
|
||||
}
|
||||
}
|
||||
unset($instance);
|
||||
} else {
|
||||
// Simple associative array
|
||||
// Simple associative array - add field
|
||||
$grouped[$entity][$field] = $value;
|
||||
}
|
||||
}
|
||||
@@ -634,7 +664,7 @@ protected function rowIsEffectivelyEmpty(array $assoc): bool
|
||||
*
|
||||
* Updated to match ImportServiceV2:
|
||||
* - Supports group option for concatenating multiple sources
|
||||
* - Uses setNestedValue for proper array handling
|
||||
* - Returns flat array with "entity.field" keys (no nesting)
|
||||
*/
|
||||
protected function applyMappings(array $raw, array $mappings): array
|
||||
{
|
||||
@@ -685,16 +715,32 @@ protected function applyMappings(array $raw, array $mappings): array
|
||||
}
|
||||
}
|
||||
|
||||
// Now set the values (same logic as ImportServiceV2)
|
||||
// Now set the values - KEEP FLAT, DON'T NEST
|
||||
foreach ($valuesByGroup as $values) {
|
||||
if (count($values) === 1) {
|
||||
// Single value - set directly
|
||||
$this->setNestedValue($mapped, $targetField, $values[0]);
|
||||
// Single value - add to array if key exists, otherwise set directly
|
||||
if (isset($mapped[$targetField])) {
|
||||
// Convert to array and append
|
||||
if (!is_array($mapped[$targetField])) {
|
||||
$mapped[$targetField] = [$mapped[$targetField]];
|
||||
}
|
||||
$mapped[$targetField][] = $values[0];
|
||||
} else {
|
||||
$mapped[$targetField] = $values[0];
|
||||
}
|
||||
} else {
|
||||
// Multiple values in same group - concatenate with newline
|
||||
$concatenated = implode("\n", array_filter($values, fn($v) => !empty($v) && trim((string)$v) !== ''));
|
||||
if (!empty($concatenated)) {
|
||||
$this->setNestedValue($mapped, $targetField, $concatenated);
|
||||
if (isset($mapped[$targetField])) {
|
||||
// Convert to array and append
|
||||
if (!is_array($mapped[$targetField])) {
|
||||
$mapped[$targetField] = [$mapped[$targetField]];
|
||||
}
|
||||
$mapped[$targetField][] = $concatenated;
|
||||
} else {
|
||||
$mapped[$targetField] = $concatenated;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user