5
5
*/
6
6
namespace Magento \Catalog \Model \Indexer \Product \Flat ;
7
7
8
- use Magento \Framework \ App \ ResourceConnection ;
8
+ use Magento \Catalog \ Model \ Indexer \ Product \ Flat \ Table \ BuilderInterfaceFactory ;
9
9
10
10
/**
11
11
* Class TableBuilder
@@ -32,6 +32,11 @@ class TableBuilder
32
32
*/
33
33
protected $ resource ;
34
34
35
+ /**
36
+ * @var BuilderInterfaceFactory
37
+ */
38
+ private $ tableBuilderFactory ;
39
+
35
40
/**
36
41
* Check whether builder was executed
37
42
*
@@ -123,17 +128,27 @@ protected function _createTemporaryTable($tableName, array $columns, $valueField
123
128
$ valueTables = [];
124
129
if (!empty ($ columns )) {
125
130
$ valueTableName = $ tableName . $ valueFieldSuffix ;
126
- $ temporaryTable = $ this ->_connection ->newTable ($ tableName );
127
- $ valueTemporaryTable = $ this ->_connection ->newTable ($ valueTableName );
131
+ $ temporaryTableBuilder = $ this ->getTableBuilderFactory ()->create (
132
+ [
133
+ 'connection ' => $ this ->_connection ,
134
+ 'tableName ' => $ tableName
135
+ ]
136
+ );
137
+ $ valueTemporaryTableBuilder = $ this ->getTableBuilderFactory ()->create (
138
+ [
139
+ 'connection ' => $ this ->_connection ,
140
+ 'tableName ' => $ valueTableName
141
+ ]
142
+ );
128
143
$ flatColumns = $ this ->_productIndexerHelper ->getFlatColumns ();
129
144
130
- $ temporaryTable ->addColumn ('entity_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
145
+ $ temporaryTableBuilder ->addColumn ('entity_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
131
146
132
- $ temporaryTable ->addColumn ('type_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_TEXT );
147
+ $ temporaryTableBuilder ->addColumn ('type_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_TEXT );
133
148
134
- $ temporaryTable ->addColumn ('attribute_set_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
149
+ $ temporaryTableBuilder ->addColumn ('attribute_set_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
135
150
136
- $ valueTemporaryTable ->addColumn ('entity_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
151
+ $ valueTemporaryTableBuilder ->addColumn ('entity_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
137
152
138
153
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
139
154
foreach ($ columns as $ columnName => $ attribute ) {
@@ -145,7 +160,7 @@ protected function _createTemporaryTable($tableName, array $columns, $valueField
145
160
$ column = $ column [$ attributeCode ];
146
161
}
147
162
148
- $ temporaryTable ->addColumn (
163
+ $ temporaryTableBuilder ->addColumn (
149
164
$ columnName ,
150
165
$ column ['type ' ],
151
166
isset ($ column ['length ' ]) ? $ column ['length ' ] : null
@@ -154,19 +169,19 @@ protected function _createTemporaryTable($tableName, array $columns, $valueField
154
169
$ columnValueName = $ attributeCode . $ valueFieldSuffix ;
155
170
if (isset ($ flatColumns [$ columnValueName ])) {
156
171
$ columnValue = $ flatColumns [$ columnValueName ];
157
- $ valueTemporaryTable ->addColumn (
172
+ $ valueTemporaryTableBuilder ->addColumn (
158
173
$ columnValueName ,
159
174
$ columnValue ['type ' ],
160
175
isset ($ columnValue ['length ' ]) ? $ columnValue ['length ' ] : null
161
176
);
162
177
}
163
178
}
164
179
$ this ->_connection ->dropTemporaryTable ($ tableName );
165
- $ this ->_connection ->createTemporaryTable ($ temporaryTable );
180
+ $ this ->_connection ->createTemporaryTable ($ temporaryTableBuilder -> getTable () );
166
181
167
- if (count ($ valueTemporaryTable ->getColumns ()) > 1 ) {
182
+ if (count ($ valueTemporaryTableBuilder -> getTable () ->getColumns ()) > 1 ) {
168
183
$ this ->_connection ->dropTemporaryTable ($ valueTableName );
169
- $ this ->_connection ->createTemporaryTable ($ valueTemporaryTable );
184
+ $ this ->_connection ->createTemporaryTable ($ valueTemporaryTableBuilder -> getTable () );
170
185
$ valueTables [$ valueTableName ] = $ valueTableName ;
171
186
}
172
187
}
@@ -197,7 +212,8 @@ protected function _fillTemporaryEntityTable($tableName, array $columns, array $
197
212
if (!empty ($ columns )) {
198
213
$ select = $ this ->_connection ->select ();
199
214
$ temporaryEntityTable = $ this ->_getTemporaryTableName ($ tableName );
200
- $ idsColumns = ['entity_id ' , 'type_id ' , 'attribute_set_id ' ];
215
+ $ metadata = $ this ->getMetadataPool ()->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class);
216
+ $ idsColumns = array_unique ([$ metadata ->getLinkField (), 'entity_id ' , 'type_id ' , 'attribute_set_id ' ]);
201
217
202
218
$ columns = array_merge ($ idsColumns , array_keys ($ columns ));
203
219
@@ -261,7 +277,7 @@ protected function _fillTemporaryTable(
261
277
);
262
278
$ temporaryTableName = $ this ->_getTemporaryTableName ($ tableName );
263
279
$ temporaryValueTableName = $ temporaryTableName . $ valueFieldSuffix ;
264
- $ keyColumn = [ 'entity_id ' ];
280
+ $ keyColumn = array_unique ([ $ metadata -> getLinkField (), 'entity_id ' ]) ;
265
281
$ columns = array_merge ($ keyColumn , array_keys ($ columnsList ));
266
282
$ valueColumns = $ keyColumn ;
267
283
$ flatColumns = $ this ->_productIndexerHelper ->getFlatColumns ();
@@ -333,6 +349,19 @@ protected function _fillTemporaryTable(
333
349
}
334
350
}
335
351
352
+ /**
353
+ * @return BuilderInterfaceFactory
354
+ */
355
+ private function getTableBuilderFactory ()
356
+ {
357
+ if (null === $ this ->tableBuilderFactory ) {
358
+ $ this ->tableBuilderFactory = \Magento \Framework \App \ObjectManager::getInstance ()
359
+ ->get (BuilderInterfaceFactory::class);
360
+ }
361
+
362
+ return $ this ->tableBuilderFactory ;
363
+ }
364
+
336
365
/**
337
366
* @return \Magento\Framework\EntityManager\MetadataPool
338
367
*/
0 commit comments