7
7
8
8
use Magento \Catalog \Api \CategoryRepositoryInterface ;
9
9
use Magento \Framework \Exception \NoSuchEntityException ;
10
+ use Magento \Store \Model \Store ;
10
11
11
12
class Rows extends \Magento \Catalog \Model \Indexer \Category \Flat \AbstractAction
12
13
{
@@ -34,11 +35,11 @@ public function __construct(
34
35
/**
35
36
* Return index table name
36
37
*
37
- * @param \Magento\Store\Model\ Store $store
38
+ * @param Store $store
38
39
* @param bool $useTempTable
39
40
* @return string
40
41
*/
41
- protected function getTableNameByStore (\ Magento \ Store \ Model \ Store $ store , $ useTempTable )
42
+ protected function getTableNameByStore (Store $ store , $ useTempTable )
42
43
{
43
44
$ tableName = $ this ->getMainStoreTable ($ store ->getId ());
44
45
return $ useTempTable ? $ this ->addTemporaryTableSuffix ($ tableName ) : $ tableName ;
@@ -55,50 +56,8 @@ public function reindex(array $entityIds = [], $useTempTable = false)
55
56
{
56
57
$ stores = $ this ->storeManager ->getStores ();
57
58
58
- /* @var $store \Magento\Store\Model\Store */
59
59
foreach ($ stores as $ store ) {
60
- $ tableName = $ this ->getTableNameByStore ($ store , $ useTempTable );
61
-
62
- if (!$ this ->connection ->isTableExists ($ tableName )) {
63
- continue ;
64
- }
65
-
66
- /** @TODO Do something with chunks */
67
- $ categoriesIdsChunks = array_chunk ($ entityIds , 500 );
68
- foreach ($ categoriesIdsChunks as $ categoriesIdsChunk ) {
69
- $ categoriesIdsChunk = $ this ->filterIdsByStore ($ categoriesIdsChunk , $ store );
70
-
71
- $ attributesData = $ this ->getAttributeValues ($ categoriesIdsChunk , $ store ->getId ());
72
- $ data = [];
73
- foreach ($ categoriesIdsChunk as $ categoryId ) {
74
- if (!isset ($ attributesData [$ categoryId ])) {
75
- continue ;
76
- }
77
-
78
- try {
79
- $ category = $ this ->categoryRepository ->get ($ categoryId );
80
- } catch (NoSuchEntityException $ e ) {
81
- continue ;
82
- }
83
-
84
- $ data [] = $ this ->prepareValuesToInsert (
85
- array_merge (
86
- $ category ->getData (),
87
- $ attributesData [$ categoryId ],
88
- ['store_id ' => $ store ->getId ()]
89
- )
90
- );
91
- }
92
-
93
- foreach ($ data as $ row ) {
94
- $ updateFields = [];
95
- foreach (array_keys ($ row ) as $ key ) {
96
- $ updateFields [$ key ] = $ key ;
97
- }
98
- $ this ->connection ->insertOnDuplicate ($ tableName , $ row , $ updateFields );
99
- }
100
- }
101
- $ this ->deleteNonStoreCategories ($ store , $ useTempTable );
60
+ $ this ->reindexStore ($ store , $ entityIds , $ useTempTable );
102
61
}
103
62
104
63
return $ this ;
@@ -107,11 +66,11 @@ public function reindex(array $entityIds = [], $useTempTable = false)
107
66
/**
108
67
* Delete non stores categories
109
68
*
110
- * @param \Magento\Store\Model\ Store $store
69
+ * @param Store $store
111
70
* @param bool $useTempTable
112
71
* @return void
113
72
*/
114
- protected function deleteNonStoreCategories (\ Magento \ Store \ Model \ Store $ store , $ useTempTable )
73
+ protected function deleteNonStoreCategories (Store $ store , $ useTempTable )
115
74
{
116
75
$ rootId = \Magento \Catalog \Model \Category::TREE_ROOT_ID ;
117
76
@@ -142,7 +101,7 @@ protected function deleteNonStoreCategories(\Magento\Store\Model\Store $store, $
142
101
* Filter category ids by store
143
102
*
144
103
* @param int[] $ids
145
- * @param \Magento\Store\Model\ Store $store
104
+ * @param Store $store
146
105
* @return int[]
147
106
*/
148
107
protected function filterIdsByStore (array $ ids , $ store )
@@ -169,4 +128,97 @@ protected function filterIdsByStore(array $ids, $store)
169
128
}
170
129
return $ resultIds ;
171
130
}
131
+
132
+ /**
133
+ * Reindex data for store
134
+ *
135
+ * @param Store $store
136
+ * @param int[] $entityIds
137
+ * @param bool $useTempTable
138
+ */
139
+ private function reindexStore (Store $ store , array $ entityIds , $ useTempTable )
140
+ {
141
+ $ tableName = $ this ->getTableNameByStore ($ store , $ useTempTable );
142
+ if (!$ this ->connection ->isTableExists ($ tableName )) {
143
+ return ;
144
+ }
145
+
146
+ $ categoriesIdsChunks = array_chunk ($ entityIds , 500 );
147
+ foreach ($ categoriesIdsChunks as $ categoriesIdsChunk ) {
148
+ $ categoriesIdsChunk = $ this ->filterIdsByStore ($ categoriesIdsChunk , $ store );
149
+ $ attributesData = $ this ->getAttributeValues ($ categoriesIdsChunk , $ store ->getId ());
150
+ $ indexData = $ this ->buildIndexData ($ store , $ categoriesIdsChunk , $ attributesData );
151
+ $ this ->updateIndexData ($ tableName , $ indexData );
152
+ }
153
+
154
+ $ this ->deleteNonStoreCategories ($ store , $ useTempTable );
155
+ }
156
+
157
+ /**
158
+ * Build data for insert into index
159
+ *
160
+ * @param Store $store
161
+ * @param int[] $categoriesIdsChunk
162
+ * @param array[] $attributesData
163
+ * @return array
164
+ */
165
+ private function buildIndexData (Store $ store , $ categoriesIdsChunk , $ attributesData )
166
+ {
167
+ $ data = [];
168
+ foreach ($ categoriesIdsChunk as $ categoryId ) {
169
+ try {
170
+ $ categoryAttributesData = [];
171
+ if (isset ($ attributesData [$ categoryId ]) && is_array ($ attributesData [$ categoryId ])) {
172
+ $ categoryAttributesData = $ attributesData [$ categoryId ];
173
+ }
174
+ $ categoryIndexData = $ this ->buildCategoryIndexData (
175
+ $ store ,
176
+ $ categoryId ,
177
+ $ categoryAttributesData
178
+ );
179
+ $ data [] = $ categoryIndexData ;
180
+ } catch (NoSuchEntityException $ e ) {
181
+ // ignore
182
+ }
183
+ }
184
+ return $ data ;
185
+ }
186
+
187
+ /**
188
+ * @param Store $store
189
+ * @param int $categoryId
190
+ * @param array $categoryAttributesData
191
+ * @return array
192
+ * @throws NoSuchEntityException
193
+ */
194
+ private function buildCategoryIndexData (Store $ store , $ categoryId , array $ categoryAttributesData )
195
+ {
196
+ $ category = $ this ->categoryRepository ->get ($ categoryId );
197
+ $ categoryAttributesData = [];
198
+ $ data = $ this ->prepareValuesToInsert (
199
+ array_merge (
200
+ $ category ->getData (),
201
+ $ categoryAttributesData ,
202
+ ['store_id ' => $ store ->getId ()]
203
+ )
204
+ );
205
+ return $ data ;
206
+ }
207
+
208
+ /**
209
+ * Insert or update index data
210
+ *
211
+ * @param string $tableName
212
+ * @param $data
213
+ */
214
+ private function updateIndexData ($ tableName , $ data )
215
+ {
216
+ foreach ($ data as $ row ) {
217
+ $ updateFields = [];
218
+ foreach (array_keys ($ row ) as $ key ) {
219
+ $ updateFields [$ key ] = $ key ;
220
+ }
221
+ $ this ->connection ->insertOnDuplicate ($ tableName , $ row , $ updateFields );
222
+ }
223
+ }
172
224
}
0 commit comments