55 */
66namespace Magento \Catalog \Model \Indexer \Product \Flat ;
77
8- use Magento \Framework \ App \ ResourceConnection ;
8+ use Magento \Catalog \ Model \ Indexer \ Product \ Flat \ Table \ BuilderInterfaceFactory ;
99
1010/**
1111 * Class TableBuilder
@@ -32,6 +32,11 @@ class TableBuilder
3232 */
3333 protected $ resource ;
3434
35+ /**
36+ * @var BuilderInterfaceFactory
37+ */
38+ private $ tableBuilderFactory ;
39+
3540 /**
3641 * Check whether builder was executed
3742 *
@@ -123,17 +128,27 @@ protected function _createTemporaryTable($tableName, array $columns, $valueField
123128 $ valueTables = [];
124129 if (!empty ($ columns )) {
125130 $ 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+ );
128143 $ flatColumns = $ this ->_productIndexerHelper ->getFlatColumns ();
129144
130- $ temporaryTable ->addColumn ('entity_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
145+ $ temporaryTableBuilder ->addColumn ('entity_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
131146
132- $ temporaryTable ->addColumn ('type_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_TEXT );
147+ $ temporaryTableBuilder ->addColumn ('type_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_TEXT );
133148
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 );
135150
136- $ valueTemporaryTable ->addColumn ('entity_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
151+ $ valueTemporaryTableBuilder ->addColumn ('entity_id ' , \Magento \Framework \DB \Ddl \Table::TYPE_INTEGER );
137152
138153 /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
139154 foreach ($ columns as $ columnName => $ attribute ) {
@@ -145,7 +160,7 @@ protected function _createTemporaryTable($tableName, array $columns, $valueField
145160 $ column = $ column [$ attributeCode ];
146161 }
147162
148- $ temporaryTable ->addColumn (
163+ $ temporaryTableBuilder ->addColumn (
149164 $ columnName ,
150165 $ column ['type ' ],
151166 isset ($ column ['length ' ]) ? $ column ['length ' ] : null
@@ -154,19 +169,19 @@ protected function _createTemporaryTable($tableName, array $columns, $valueField
154169 $ columnValueName = $ attributeCode . $ valueFieldSuffix ;
155170 if (isset ($ flatColumns [$ columnValueName ])) {
156171 $ columnValue = $ flatColumns [$ columnValueName ];
157- $ valueTemporaryTable ->addColumn (
172+ $ valueTemporaryTableBuilder ->addColumn (
158173 $ columnValueName ,
159174 $ columnValue ['type ' ],
160175 isset ($ columnValue ['length ' ]) ? $ columnValue ['length ' ] : null
161176 );
162177 }
163178 }
164179 $ this ->_connection ->dropTemporaryTable ($ tableName );
165- $ this ->_connection ->createTemporaryTable ($ temporaryTable );
180+ $ this ->_connection ->createTemporaryTable ($ temporaryTableBuilder -> getTable () );
166181
167- if (count ($ valueTemporaryTable ->getColumns ()) > 1 ) {
182+ if (count ($ valueTemporaryTableBuilder -> getTable () ->getColumns ()) > 1 ) {
168183 $ this ->_connection ->dropTemporaryTable ($ valueTableName );
169- $ this ->_connection ->createTemporaryTable ($ valueTemporaryTable );
184+ $ this ->_connection ->createTemporaryTable ($ valueTemporaryTableBuilder -> getTable () );
170185 $ valueTables [$ valueTableName ] = $ valueTableName ;
171186 }
172187 }
@@ -197,7 +212,8 @@ protected function _fillTemporaryEntityTable($tableName, array $columns, array $
197212 if (!empty ($ columns )) {
198213 $ select = $ this ->_connection ->select ();
199214 $ 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 ' ]);
201217
202218 $ columns = array_merge ($ idsColumns , array_keys ($ columns ));
203219
@@ -261,7 +277,7 @@ protected function _fillTemporaryTable(
261277 );
262278 $ temporaryTableName = $ this ->_getTemporaryTableName ($ tableName );
263279 $ temporaryValueTableName = $ temporaryTableName . $ valueFieldSuffix ;
264- $ keyColumn = [ 'entity_id ' ];
280+ $ keyColumn = array_unique ([ $ metadata -> getLinkField (), 'entity_id ' ]) ;
265281 $ columns = array_merge ($ keyColumn , array_keys ($ columnsList ));
266282 $ valueColumns = $ keyColumn ;
267283 $ flatColumns = $ this ->_productIndexerHelper ->getFlatColumns ();
@@ -333,6 +349,19 @@ protected function _fillTemporaryTable(
333349 }
334350 }
335351
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+
336365 /**
337366 * @return \Magento\Framework\EntityManager\MetadataPool
338367 */
0 commit comments