Skip to content

Commit 46c1d11

Browse files
committed
MC-35044: Error Duplicate entry 31-Images during migration on EAV Step
1 parent ba75d3c commit 46c1d11

File tree

5 files changed

+70
-57
lines changed

5 files changed

+70
-57
lines changed

src/Migration/Step/Eav/Data.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ private function createProductAttributeSetStructures()
307307
);
308308
// add default attribute groups from Magento 2 for each attribute set from Magento 1
309309
$prototypeProductAttributeGroups = $this->modelData->getDefaultProductAttributeGroups();
310-
$productAttributeSets = $this->modelData->getAttributeSets(
311-
$this->modelData->getEntityTypeIdByCode(ModelData::ENTITY_TYPE_PRODUCT_CODE),
310+
$productAttributeSets = $this->modelData->getProductAttributeSets(
312311
ModelData::ATTRIBUTE_SETS_NONE_DEFAULT
313312
);
314313
foreach ($productAttributeSets as $attributeSet) {
@@ -347,10 +346,7 @@ private function createProductAttributeSetStructures()
347346
public function migrateCustomProductAttributeGroups()
348347
{
349348
$this->progress->advance();
350-
$productAttributeSets = $this->modelData->getAttributeSets(
351-
$this->modelData->getEntityTypeIdByCode(ModelData::ENTITY_TYPE_PRODUCT_CODE),
352-
ModelData::ATTRIBUTE_SETS_ALL
353-
);
349+
$productAttributeSets = $this->modelData->getProductAttributeSets();
354350
foreach ($productAttributeSets as $productAttributeSet) {
355351
$attributeGroupIds = $this->modelData->getCustomProductAttributeGroups(
356352
$productAttributeSet['attribute_set_id']
@@ -407,7 +403,9 @@ private function migrateAttributes()
407403
$this->map->getDocumentMap($sourceDocName, MapInterface::TYPE_SOURCE)
408404
);
409405
$this->destination->backupDocument($destinationDocument->getName());
410-
$sourceRecords = $this->ignoredAttributes->clearIgnoredAttributes($this->initialData->getAttributes('source'));
406+
$sourceRecords = $this->ignoredAttributes->clearIgnoredAttributes(
407+
$this->initialData->getAttributes(ModelData::TYPE_SOURCE)
408+
);
411409
$destinationRecords = $this->helper->getDestinationRecords(
412410
$sourceDocName,
413411
['entity_type_id', 'attribute_code']
@@ -598,12 +596,12 @@ private function createMapEntityTypeIds()
598596
'eav_entity_type',
599597
['entity_type_code']
600598
);
601-
foreach ($this->initialData->getEntityTypes('dest') as $entityTypeIdOld => $recordOld) {
599+
foreach ($this->initialData->getEntityTypes(ModelData::TYPE_DEST) as $entityTypeIdOld => $recordOld) {
602600
$entityTypeMigrated = $entityTypesMigrated[$recordOld['entity_type_code']];
603601
$this->mapEntityTypeIdsDestOldNew[$entityTypeIdOld] = $entityTypeMigrated['entity_type_id'];
604602
}
605-
foreach ($this->initialData->getEntityTypes('source') as $entityTypeIdSource => $recordSource) {
606-
foreach ($this->initialData->getEntityTypes('dest') as $entityTypeIdDest => $recordDest) {
603+
foreach ($this->initialData->getEntityTypes(ModelData::TYPE_SOURCE) as $entityTypeIdSource => $recordSource) {
604+
foreach ($this->initialData->getEntityTypes(ModelData::TYPE_DEST) as $entityTypeIdDest => $recordDest) {
607605
if ($recordSource['entity_type_code'] == $recordDest['entity_type_code']) {
608606
$this->mapEntityTypeIdsSourceDest[$entityTypeIdSource] = $entityTypeIdDest;
609607
}
@@ -622,7 +620,7 @@ private function createMapAttributeSetIds()
622620
'eav_attribute_set',
623621
['entity_type_id', 'attribute_set_name']
624622
);
625-
foreach ($this->initialData->getAttributeSets('dest') as $attributeSetId => $record) {
623+
foreach ($this->initialData->getAttributeSets(ModelData::TYPE_DEST) as $attributeSetId => $record) {
626624
$entityTypeId = $this->mapEntityTypeIdsDestOldNew[$record['entity_type_id']];
627625
$newAttributeSet = $this->newAttributeSets[$entityTypeId . '-' . $record['attribute_set_name']];
628626
$this->mapAttributeSetIdsDestOldNew[$attributeSetId] = $newAttributeSet['attribute_set_id'];
@@ -641,18 +639,18 @@ private function createMapAttributeIds()
641639
'eav_attribute',
642640
['entity_type_id', 'attribute_code']
643641
);
644-
foreach ($this->initialData->getAttributes('dest') as $keyOld => $attributeOld) {
642+
foreach ($this->initialData->getAttributes(ModelData::TYPE_DEST) as $keyOld => $attributeOld) {
645643
$entityTypeId = $attributeOld['entity_type_id'];
646644
$attributeCode = $attributeOld['attribute_code'];
647645
$keyMapped = $this->mapEntityTypeIdsDestOldNew[$entityTypeId] . '-' . $attributeCode;
648646
$this->mapAttributeIdsDestOldNew[$attributeOld['attribute_id']] =
649647
$newAttributes[$keyMapped]['attribute_id'];
650648
}
651-
foreach ($this->initialData->getAttributes('source') as $recordSourceId => $recordSource) {
652-
foreach ($this->initialData->getAttributes('dest') as $recordDestId => $recordDest) {
653-
$sourceEntityTypeCode = $this->initialData->getEntityTypes('source')
649+
foreach ($this->initialData->getAttributes(ModelData::TYPE_SOURCE) as $recordSourceId => $recordSource) {
650+
foreach ($this->initialData->getAttributes(ModelData::TYPE_DEST) as $recordDestId => $recordDest) {
651+
$sourceEntityTypeCode = $this->initialData->getEntityTypes(ModelData::TYPE_SOURCE)
654652
[$recordSource['entity_type_id']]['entity_type_code'];
655-
$destinationEntityTypeCode = $this->initialData->getEntityTypes('dest')
653+
$destinationEntityTypeCode = $this->initialData->getEntityTypes(ModelData::TYPE_DEST)
656654
[$recordDest['entity_type_id']]['entity_type_code'];
657655
if ($recordSource['attribute_code'] == $recordDest['attribute_code']
658656
&& $sourceEntityTypeCode == $destinationEntityTypeCode
@@ -676,10 +674,7 @@ private function createMapProductAttributeGroupIds()
676674
'eav_attribute_group',
677675
['attribute_group_id']
678676
);
679-
$productAttributeSetIds = array_keys($this->modelData->getAttributeSets(
680-
$this->modelData->getEntityTypeIdByCode(ModelData::ENTITY_TYPE_PRODUCT_CODE),
681-
ModelData::ATTRIBUTE_SETS_ALL
682-
));
677+
$productAttributeSetIds = array_keys($this->modelData->getProductAttributeSets());
683678
foreach ($attributeGroupsSource as $idSource => $recordSource) {
684679
$sourceAttributeGroupName = $recordSource['attribute_group_name'];
685680
if (in_array($recordSource['attribute_set_id'], $productAttributeSetIds)) {

src/Migration/Step/Eav/InitialData.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Migration\Reader\Map;
1010
use Migration\ResourceModel\Destination;
1111
use Migration\ResourceModel\Source;
12+
use Migration\Step\Eav\Model\Data as ModelData;
1213

1314
/**
1415
* Class InitialData
@@ -90,8 +91,10 @@ private function initEntityTypes()
9091
if ($this->entityTypes) {
9192
return;
9293
}
93-
$this->entityTypes['source'] = $this->helper->getSourceRecords('eav_entity_type', ['entity_type_id']);
94-
$this->entityTypes['dest'] = $this->helper->getDestinationRecords('eav_entity_type', ['entity_type_id']);
94+
$this->entityTypes[ModelData::TYPE_SOURCE] =
95+
$this->helper->getSourceRecords('eav_entity_type', ['entity_type_id']);
96+
$this->entityTypes[ModelData::TYPE_DEST] =
97+
$this->helper->getDestinationRecords('eav_entity_type', ['entity_type_id']);
9598
}
9699

97100
/**
@@ -106,10 +109,10 @@ private function initAttributes()
106109
}
107110
$sourceDocument = 'eav_attribute';
108111
foreach ($this->helper->getSourceRecords($sourceDocument, ['attribute_id']) as $id => $record) {
109-
$this->attributes['source'][$id] = $record;
112+
$this->attributes[ModelData::TYPE_SOURCE][$id] = $record;
110113
}
111114
foreach ($this->helper->getDestinationRecords($sourceDocument, ['attribute_id']) as $id => $record) {
112-
$this->attributes['dest'][$id] = $record;
115+
$this->attributes[ModelData::TYPE_DEST][$id] = $record;
113116
}
114117
}
115118

@@ -123,11 +126,11 @@ private function initAttributeSets()
123126
if ($this->attributeSets) {
124127
return;
125128
}
126-
$this->attributeSets['source'] = $this->helper->getSourceRecords(
129+
$this->attributeSets[ModelData::TYPE_SOURCE] = $this->helper->getSourceRecords(
127130
'eav_attribute_set',
128131
['attribute_set_id']
129132
);
130-
$this->attributeSets['dest'] = $this->helper->getDestinationRecords(
133+
$this->attributeSets[ModelData::TYPE_DEST] = $this->helper->getDestinationRecords(
131134
'eav_attribute_set',
132135
['attribute_set_id']
133136
);
@@ -143,11 +146,11 @@ private function initAttributeGroups()
143146
if ($this->attributeGroups) {
144147
return;
145148
}
146-
$this->attributeGroups['source'] = $this->helper->getSourceRecords(
149+
$this->attributeGroups[ModelData::TYPE_SOURCE] = $this->helper->getSourceRecords(
147150
'eav_attribute_group',
148151
['attribute_group_id']
149152
);
150-
$this->attributeGroups['dest'] = $this->helper->getDestinationRecords(
153+
$this->attributeGroups[ModelData::TYPE_DEST] = $this->helper->getDestinationRecords(
151154
'eav_attribute_group',
152155
['attribute_group_id']
153156
);
@@ -163,11 +166,11 @@ private function initEntityAttribute()
163166
if ($this->entityAttributes) {
164167
return;
165168
}
166-
$this->entityAttributes['source'] = $this->helper->getSourceRecords(
169+
$this->entityAttributes[ModelData::TYPE_SOURCE] = $this->helper->getSourceRecords(
167170
'eav_entity_attribute',
168171
['entity_attribute_id']
169172
);
170-
$this->entityAttributes['dest'] = $this->helper->getDestinationRecords(
173+
$this->entityAttributes[ModelData::TYPE_DEST] = $this->helper->getDestinationRecords(
171174
'eav_entity_attribute',
172175
['entity_attribute_id']
173176
);

src/Migration/Step/Eav/Model/Data.php

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class Data
5858
const ENTITY_TYPE_CUSTOMER_CODE = 'customer';
5959
const ENTITY_TYPE_CUSTOMER_ADDRESS_CODE = 'customer_address';
6060

61+
const TYPE_SOURCE = 'source';
62+
const TYPE_DEST = 'destination';
63+
6164
/**
6265
* @param Source $source
6366
* @param Destination $destination
@@ -105,19 +108,20 @@ public function updateMappedKeys($destDocument, $column, $records, $mappedIdKeys
105108
}
106109

107110
/**
108-
* Get list of attribute sets depending on entity type
111+
* Get list of product attribute sets
109112
*
110-
* @param null|int $entityTypeId
111113
* @param string $mode
114+
* @param string $type
112115
* @return array|mixed
113116
*/
114-
public function getAttributeSets($entityTypeId = null, $mode = self::ATTRIBUTE_SETS_ALL)
115-
{
117+
public function getProductAttributeSets(
118+
$mode = self::ATTRIBUTE_SETS_ALL,
119+
$type = self::TYPE_SOURCE
120+
) {
121+
$productEntityTypeId = $this->getEntityTypeIdByCode(self::ENTITY_TYPE_PRODUCT_CODE, $type);
116122
$attributeSets = [];
117-
foreach ($this->initialData->getAttributeSets('source') as $attributeSet) {
118-
if ($entityTypeId === null
119-
|| $entityTypeId == $attributeSet['entity_type_id']
120-
) {
123+
foreach ($this->initialData->getAttributeSets($type) as $attributeSet) {
124+
if ($productEntityTypeId == $attributeSet['entity_type_id']) {
121125
$attributeSets[$attributeSet['attribute_set_id']] = $attributeSet;
122126
}
123127
}
@@ -133,13 +137,14 @@ public function getAttributeSets($entityTypeId = null, $mode = self::ATTRIBUTE_S
133137
/**
134138
* Return entity type id by its code
135139
*
136-
* @param $code
140+
* @param string $code
141+
* @param string $type
137142
* @return mixed|null
138143
*/
139-
public function getEntityTypeIdByCode($code)
144+
public function getEntityTypeIdByCode($code, $type = self::TYPE_SOURCE)
140145
{
141146
$entityTypeId = null;
142-
foreach ($this->initialData->getEntityTypes('source') as $entityType) {
147+
foreach ($this->initialData->getEntityTypes($type) as $entityType) {
143148
if ($entityType['entity_type_code'] == $code) {
144149
$entityTypeId = $entityType['entity_type_id'];
145150
}
@@ -154,12 +159,12 @@ public function getEntityTypeIdByCode($code)
154159
*/
155160
public function getDefaultProductAttributeGroups()
156161
{
157-
$defaultProductAttributeSetId = $this->getAttributeSets(
158-
$this->getEntityTypeIdByCode(self::ENTITY_TYPE_PRODUCT_CODE),
159-
self::ATTRIBUTE_SETS_DEFAULT
162+
$defaultProductAttributeSetId = $this->getProductAttributeSets(
163+
self::ATTRIBUTE_SETS_DEFAULT,
164+
self::TYPE_DEST
160165
)['attribute_set_id'];
161166
$attributeGroups = [];
162-
foreach ($this->initialData->getAttributeGroups('dest') as $attributeGroup) {
167+
foreach ($this->initialData->getAttributeGroups(self::TYPE_DEST) as $attributeGroup) {
163168
if ($attributeGroup['attribute_set_id'] == $defaultProductAttributeSetId) {
164169
$attributeGroup['attribute_group_id'] = null;
165170
$attributeGroup['attribute_set_id'] = null;
@@ -176,12 +181,12 @@ public function getDefaultProductAttributeGroups()
176181
*/
177182
public function getDefaultProductEntityAttributes()
178183
{
179-
$defaultProductAttributeSetId = $this->getAttributeSets(
180-
$this->getEntityTypeIdByCode(self::ENTITY_TYPE_PRODUCT_CODE),
181-
self::ATTRIBUTE_SETS_DEFAULT
184+
$defaultProductAttributeSetId = $this->getProductAttributeSets(
185+
self::ATTRIBUTE_SETS_DEFAULT,
186+
self::TYPE_DEST
182187
)['attribute_set_id'];
183188
$entityAttributes = [];
184-
foreach ($this->initialData->getEntityAttributes('dest') as $entityAttribute) {
189+
foreach ($this->initialData->getEntityAttributes(self::TYPE_DEST) as $entityAttribute) {
185190
if ($entityAttribute['attribute_set_id'] == $defaultProductAttributeSetId) {
186191
$entityAttribute['entity_attribute_id'] = null;
187192
$entityAttribute['attribute_set_id'] = null;
@@ -221,7 +226,7 @@ public function getAttributeGroupIdForAttributeSet($prototypeAttributeGroupId, $
221226
public function getDestAttributeGroupCodeFromId($attributeGroupId)
222227
{
223228
$attributeGroupCode = null;
224-
foreach ($this->initialData->getAttributeGroups('dest') as $attributeGroup) {
229+
foreach ($this->initialData->getAttributeGroups(self::TYPE_DEST) as $attributeGroup) {
225230
if ($attributeGroup['attribute_group_id'] == $attributeGroupId) {
226231
$attributeGroupCode = $attributeGroup['attribute_group_code'];
227232
}
@@ -238,7 +243,7 @@ public function getDestAttributeGroupCodeFromId($attributeGroupId)
238243
public function getSourceAttributeGroupNameFromId($attributeGroupId)
239244
{
240245
$attributeGroupName = null;
241-
foreach ($this->initialData->getAttributeGroups('source') as $attributeGroup) {
246+
foreach ($this->initialData->getAttributeGroups(self::TYPE_SOURCE) as $attributeGroup) {
242247
if ($attributeGroup['attribute_group_id'] == $attributeGroupId) {
243248
$attributeGroupName = $attributeGroup['attribute_group_name'];
244249
}
@@ -278,14 +283,22 @@ public function getCustomProductAttributeGroups($attributeSetId)
278283
public function getCustomAttributeIds()
279284
{
280285
$defaultAttributes = [];
281-
foreach ($this->initialData->getAttributes('dest') as $id => $attribute) {
282-
$defaultAttributes[$id] = $attribute['attribute_code'] . '-' . $attribute['entity_type_id'];
286+
$entityTypesSource = $this->initialData->getEntityTypes(self::TYPE_SOURCE);
287+
$entityTypesDest = $this->initialData->getEntityTypes(self::TYPE_DEST);
288+
foreach ($this->initialData->getAttributes(self::TYPE_DEST) as $id => $attribute) {
289+
$defaultAttributes[$id] =
290+
$entityTypesDest[$attribute['entity_type_id']]['entity_type_code']
291+
. '--'
292+
. $attribute['attribute_code'];
283293
}
284294
$sourceAttributes = $this->ignoredAttributes->clearIgnoredAttributes(
285-
$this->initialData->getAttributes('source')
295+
$this->initialData->getAttributes(self::TYPE_SOURCE)
286296
);
287297
foreach ($sourceAttributes as $id => $attribute) {
288-
$sourceAttributes[$id] = $attribute['attribute_code'] . '-' . $attribute['entity_type_id'];
298+
$sourceAttributes[$id] =
299+
$entityTypesSource[$attribute['entity_type_id']]['entity_type_code']
300+
. '--'
301+
. $attribute['attribute_code'];
289302
}
290303
return array_keys(array_diff($sourceAttributes, $defaultAttributes));
291304
}

src/Migration/Step/Eav/Model/IgnoredAttributes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Migration\Step\Eav\Helper;
99
use Migration\Step\Eav\InitialData;
10+
use Migration\Step\Eav\Model\Data as ModelData;
1011

1112
/**
1213
* Class IgnoredAttributes
@@ -44,7 +45,7 @@ public function __construct(
4445
public function clearIgnoredAttributes($sourceRecords)
4546
{
4647
$attributesIgnoreIds = [];
47-
$initialAttributes = $this->initialData->getAttributes('source');
48+
$initialAttributes = $this->initialData->getAttributes(ModelData::TYPE_SOURCE);
4849
$ignoredAttributes = $this->helper->getAttributesGroupCodes('ignore');
4950
foreach ($ignoredAttributes as $attributeCode => $entityTypeIds) {
5051
foreach ($initialAttributes as $attribute) {

src/Migration/Step/Eav/Volume.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Migration\Reader\GroupsFactory;
1212
use Migration\ResourceModel\Destination;
1313
use Migration\Step\Eav\Model\IgnoredAttributes;
14+
use Migration\Step\Eav\Model\Data as ModelData;
1415

1516
/**
1617
* Class Volume
@@ -105,7 +106,7 @@ private function checkAttributesMismatch()
105106
{
106107
foreach ($this->helper->getDestinationRecords('eav_attribute') as $attribute) {
107108
$sourceAttributes = $this->ignoredAttributes
108-
->clearIgnoredAttributes($this->initialData->getAttributes('source'));
109+
->clearIgnoredAttributes($this->initialData->getAttributes(ModelData::TYPE_SOURCE));
109110

110111
if (isset($sourceAttributes[$attribute['attribute_id']])
111112
&& ($sourceAttributes[$attribute['attribute_id']]['attribute_code'] != $attribute['attribute_code'])

0 commit comments

Comments
 (0)