Skip to content

Commit c993777

Browse files
author
Alexander Akimov
authored
Merge pull request #866 from magento-tsg/MAGETWO-62025-develop
[TSG] MAGETWO-62025: Upgrading from 2.0.11 with Sample Data fails
2 parents a005167 + 5c8ea22 commit c993777

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

app/code/Magento/Catalog/Setup/UpgradeData.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Api\Data\ProductAttributeInterface;
99
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
10+
use Magento\Eav\Model\Entity\AttributeCache;
1011
use Magento\Framework\Setup\UpgradeDataInterface;
1112
use Magento\Framework\Setup\ModuleContextInterface;
1213
use Magento\Framework\Setup\ModuleDataSetupInterface;
@@ -34,16 +35,26 @@ class UpgradeData implements UpgradeDataInterface
3435
*/
3536
private $eavSetupFactory;
3637

38+
/**
39+
* @var AttributeCache
40+
*/
41+
private $attributeCache;
42+
3743
/**
3844
* Init
3945
*
4046
* @param CategorySetupFactory $categorySetupFactory
4147
* @param EavSetupFactory $eavSetupFactory
48+
* @param AttributeCache $attributeCache
4249
*/
43-
public function __construct(CategorySetupFactory $categorySetupFactory, EavSetupFactory $eavSetupFactory)
44-
{
50+
public function __construct(
51+
CategorySetupFactory $categorySetupFactory,
52+
EavSetupFactory $eavSetupFactory,
53+
AttributeCache $attributeCache
54+
) {
4555
$this->categorySetupFactory = $categorySetupFactory;
4656
$this->eavSetupFactory = $eavSetupFactory;
57+
$this->attributeCache = $attributeCache;
4758
}
4859

4960
/**
@@ -135,19 +146,24 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
135146
}
136147

137148
if (version_compare($context->getVersion(), '2.0.4') < 0) {
149+
$mediaBackendType = 'static';
150+
$mediaBackendModel = null;
138151
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
139152
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
140153
$categorySetup->updateAttribute(
141154
'catalog_product',
142155
'media_gallery',
143156
'backend_type',
144-
'static'
157+
$mediaBackendType
145158
);
146159
$categorySetup->updateAttribute(
147160
'catalog_product',
148161
'media_gallery',
149-
'backend_model'
162+
'backend_model',
163+
$mediaBackendModel
150164
);
165+
166+
$this->changeMediaGalleryAttributeInCache($mediaBackendType, $mediaBackendModel);
151167
}
152168

153169
if (version_compare($context->getVersion(), '2.0.5', '<')) {
@@ -340,10 +356,10 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
340356
]
341357
);
342358
}
343-
359+
344360
if (version_compare($context->getVersion(), '2.0.7') < 0) {
345361
/** @var EavSetup $eavSetup */
346-
$eavSetup= $this->eavSetupFactory->create(['setup' => $setup]);
362+
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
347363

348364
$eavSetup->updateAttribute(
349365
ProductAttributeInterface::ENTITY_TYPE_CODE,
@@ -381,4 +397,25 @@ private function changePriceAttributeDefaultScope($categorySetup)
381397

382398
}
383399
}
400+
401+
/**
402+
* @param string $mediaBackendType
403+
* @param string $mediaBackendModel
404+
* @return void
405+
*/
406+
private function changeMediaGalleryAttributeInCache($mediaBackendType, $mediaBackendModel)
407+
{
408+
// need to do, because media_gallery has backend model in cache.
409+
$catalogProductAttributes = $this->attributeCache->getAttributes('catalog_product', '0-0');
410+
411+
if (is_array($catalogProductAttributes)) {
412+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $catalogProductAttribute */
413+
foreach ($catalogProductAttributes as $catalogProductAttribute) {
414+
if ($catalogProductAttribute->getAttributeCode() == 'media_gallery') {
415+
$catalogProductAttribute->setBackendModel($mediaBackendModel);
416+
$catalogProductAttribute->setBackendType($mediaBackendType);
417+
}
418+
}
419+
}
420+
}
384421
}

0 commit comments

Comments
 (0)