11
11
use Magento \Framework \App \Config \ScopeConfigInterface ;
12
12
use Magento \Framework \DB \Select ;
13
13
use Magento \Store \Model \ScopeInterface ;
14
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
15
+ use Magento \Framework \DB \Ddl \Table ;
14
16
15
17
/**
16
18
* Category resource collection
@@ -370,41 +372,59 @@ public function loadProductCount($items, $countRegular = true, $countAnchor = tr
370
372
* @param array $categoryIds
371
373
* @param int $websiteId
372
374
* @return array
375
+ * @throws \Zend_Db_Exception
373
376
*/
374
377
private function getCountFromCategoryTableBulk (
375
378
array $ categoryIds ,
376
379
int $ websiteId
377
380
) : array {
378
381
$ connection = $ this ->_conn ;
379
- $ tempTableName = $ connection ->getTableName ('temp_category_descendants_ ' . uniqid ());
380
- $ connection ->query ("CREATE TEMPORARY TABLE {$ tempTableName } (
381
- category_id INT UNSIGNED NOT NULL,
382
- descendant_id INT UNSIGNED NOT NULL,
383
- PRIMARY KEY (category_id, descendant_id)
384
- ) " );
385
- $ selectDescendants = clone $ connection ->select ();
386
- $ selectDescendants ->from (
387
- ['ce ' => $ this ->getTable ('catalog_category_entity ' )],
388
- ['category_id ' => 'ce.entity_id ' , 'descendant_id ' => 'ce2.entity_id ' ]
389
- )
382
+ $ tempTableName = 'temp_category_descendants_ ' . uniqid ();
383
+ $ tempTable = $ connection ->newTable ($ tempTableName )
384
+ ->addColumn (
385
+ 'category_id ' ,
386
+ Table::TYPE_INTEGER ,
387
+ null ,
388
+ ['unsigned ' => true , 'nullable ' => false ],
389
+ 'Category ID '
390
+ )
391
+ ->addColumn (
392
+ 'descendant_id ' ,
393
+ Table::TYPE_INTEGER ,
394
+ null ,
395
+ ['unsigned ' => true , 'nullable ' => false ],
396
+ 'Descendant ID '
397
+ )
398
+ ->addIndex (
399
+ $ connection ->getIndexName ($ tempTableName , ['category_id ' , 'descendant_id ' ]),
400
+ ['category_id ' , 'descendant_id ' ],
401
+ ['type ' => AdapterInterface::INDEX_TYPE_PRIMARY ]
402
+ );
403
+ $ connection ->createTemporaryTable ($ tempTable );
404
+ $ selectDescendants = $ connection ->select ()
405
+ ->from (
406
+ ['ce ' => $ this ->getTable ('catalog_category_entity ' )],
407
+ ['category_id ' => 'ce.entity_id ' , 'descendant_id ' => 'ce2.entity_id ' ]
408
+ )
390
409
->joinInner (
391
410
['ce2 ' => $ this ->getTable ('catalog_category_entity ' )],
392
411
'ce2.path LIKE CONCAT(ce.path, \'/% \') OR ce2.entity_id = ce.entity_id ' ,
393
412
[]
394
413
)
395
414
->where ('ce.entity_id IN (?) ' , $ categoryIds );
415
+
396
416
$ connection ->query (
397
417
$ connection ->insertFromSelect (
398
418
$ selectDescendants ,
399
419
$ tempTableName ,
400
420
['category_id ' , 'descendant_id ' ]
401
421
)
402
422
);
403
- $ select = clone $ connection ->select ();
404
- $ select ->from (
405
- ['t ' => $ tempTableName ],
406
- ['category_id ' => 't.category_id ' ]
407
- )
423
+ $ select = $ connection ->select ()
424
+ ->from (
425
+ ['t ' => $ tempTableName ],
426
+ ['category_id ' => 't.category_id ' ]
427
+ )
408
428
->joinLeft (
409
429
['cp ' => $ this ->getTable ('catalog_category_product ' )],
410
430
'cp.category_id = t.descendant_id ' ,
@@ -419,7 +439,7 @@ private function getCountFromCategoryTableBulk(
419
439
}
420
440
$ select ->group ('t.category_id ' );
421
441
$ result = $ connection ->fetchPairs ($ select );
422
- $ connection ->query ( " DROP TEMPORARY TABLE { $ tempTableName}" );
442
+ $ connection ->dropTemporaryTable ( $ tempTableName );
423
443
$ counts = array_fill_keys ($ categoryIds , 0 );
424
444
foreach ($ result as $ categoryId => $ count ) {
425
445
$ counts [$ categoryId ] = (int )$ count ;
0 commit comments