Skip to content

Commit 21b7fd2

Browse files
author
Alex Bomko
committed
MAGETWO-50224: Inconsistent data during installation of Store
1 parent 9115701 commit 21b7fd2

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Helper;
8+
9+
/**
10+
* Default Category helper
11+
*/
12+
class DefaultCategory
13+
{
14+
/**
15+
* Default Category ID
16+
*
17+
* @var int
18+
*/
19+
private $defaultCategoryId = 2;
20+
21+
/**
22+
* @return int
23+
*/
24+
public function getId()
25+
{
26+
return $this->defaultCategoryId;
27+
}
28+
}

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Setup\InstallDataInterface;
1010
use Magento\Framework\Setup\ModuleContextInterface;
1111
use Magento\Framework\Setup\ModuleDataSetupInterface;
12+
use Magento\Catalog\Helper\DefaultCategory;
1213

1314
/**
1415
* @codeCoverageIgnore
@@ -23,18 +24,22 @@ class InstallData implements InstallDataInterface
2324
private $categorySetupFactory;
2425

2526
/**
26-
* Root category ID
27-
*
28-
* @var int
27+
* @var DefaultCategory
2928
*/
30-
const ROOT_CATEGORY_ID = 1;
29+
private $defaultCategory;
3130

3231
/**
33-
* Default category ID
34-
*
35-
* @var int
32+
* @deprecated
33+
* @return DefaultCategory
3634
*/
37-
const DEFAULT_CATEGORY_ID = 2;
35+
private function getDefaultCategory()
36+
{
37+
if ($this->defaultCategory === null) {
38+
$this->defaultCategory = \Magento\Framework\App\ObjectManager::getInstance()
39+
->get(DefaultCategory::class);
40+
}
41+
return $this->defaultCategory;
42+
}
3843

3944
/**
4045
* Init
@@ -56,14 +61,16 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface
5661
{
5762
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
5863
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
64+
$rootCategoryId = \Magento\Catalog\Model\Category::TREE_ROOT_ID;
65+
$defaultCategoryId = $this->getDefaultCategory()->getId();
5966

6067
$categorySetup->installEntities();
6168
// Create Root Catalog Node
6269
$categorySetup->createCategory()
63-
->load(self::ROOT_CATEGORY_ID)
64-
->setId(self::ROOT_CATEGORY_ID)
70+
->load($rootCategoryId)
71+
->setId($rootCategoryId)
6572
->setStoreId(0)
66-
->setPath(self::ROOT_CATEGORY_ID)
73+
->setPath($rootCategoryId)
6774
->setLevel(0)
6875
->setPosition(0)
6976
->setChildrenCount(0)
@@ -73,10 +80,10 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface
7380

7481
// Create Default Catalog Node
7582
$category = $categorySetup->createCategory();
76-
$category->load(self::DEFAULT_CATEGORY_ID)
77-
->setId(self::DEFAULT_CATEGORY_ID)
83+
$category->load($defaultCategoryId)
84+
->setId($defaultCategoryId)
7885
->setStoreId(0)
79-
->setPath(self::ROOT_CATEGORY_ID . '/' . self::DEFAULT_CATEGORY_ID)
86+
->setPath($rootCategoryId . '/' . $defaultCategoryId)
8087
->setName('Default Category')
8188
->setDisplayMode('PRODUCTS')
8289
->setIsActive(1)

app/code/Magento/Store/Setup/InstallSchema.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,32 @@
99
use Magento\Framework\Setup\InstallSchemaInterface;
1010
use Magento\Framework\Setup\ModuleContextInterface;
1111
use Magento\Framework\Setup\SchemaSetupInterface;
12-
use \Magento\Framework\DB\Ddl\Table;
13-
use \Magento\Catalog\Setup\InstallData;
12+
use Magento\Framework\DB\Ddl\Table;
13+
use Magento\Catalog\Helper\DefaultCategory;
1414

1515
/**
1616
* @codeCoverageIgnore
1717
*/
1818
class InstallSchema implements InstallSchemaInterface
1919
{
20+
/**
21+
* @var DefaultCategory
22+
*/
23+
private $defaultCategory;
24+
25+
/**
26+
* @deprecated
27+
* @return DefaultCategory
28+
*/
29+
private function getDefaultCategory()
30+
{
31+
if ($this->defaultCategory === null) {
32+
$this->defaultCategory = \Magento\Framework\App\ObjectManager::getInstance()
33+
->get(DefaultCategory::class);
34+
}
35+
return $this->defaultCategory;
36+
}
37+
2038
/**
2139
* {@inheritdoc}
2240
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -260,7 +278,7 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
260278
'group_id' => 1,
261279
'website_id' => 1,
262280
'name' => 'Main Website Store',
263-
'root_category_id' => InstallData::DEFAULT_CATEGORY_ID,
281+
'root_category_id' => $this->getDefaultCategory()->getId(),
264282
'default_store_id' => 1
265283
]
266284
);

0 commit comments

Comments
 (0)