Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SelectBuilder.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Exception;
20 
22 {
26  private $resourceConnection;
27 
31  private $indexNameBuilder;
32 
36  private $indexNameResolver;
37 
41  private $metadataPool;
42 
49  public function __construct(
50  ResourceConnection $resourceConnection,
51  IndexNameBuilder $indexNameBuilder,
52  IndexNameResolverInterface $indexNameResolver,
53  MetadataPool $metadataPool
54  ) {
55  $this->resourceConnection = $resourceConnection;
56  $this->indexNameBuilder = $indexNameBuilder;
57  $this->indexNameResolver = $indexNameResolver;
58  $this->metadataPool = $metadataPool;
59  }
60 
68  public function execute(int $stockId): Select
69  {
70  $connection = $this->resourceConnection->getConnection();
71 
72  $indexName = $this->indexNameBuilder
73  ->setIndexId(InventoryIndexer::INDEXER_ID)
74  ->addDimension('stock_', (string)$stockId)
75  ->setAlias(Alias::ALIAS_MAIN)
76  ->build();
77 
78  $indexTableName = $this->indexNameResolver->resolveName($indexName);
79 
80  $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
81  $linkField = $metadata->getLinkField();
82 
83  $select = $connection->select()
84  ->from(
85  ['stock' => $indexTableName],
86  [
87  IndexStructure::SKU => 'parent_product_entity.sku',
88  IndexStructure::QUANTITY => 'SUM(stock.quantity)',
89  IndexStructure::IS_SALABLE => 'MAX(stock.is_salable)',
90  ]
91  )->joinInner(
92  ['product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
93  'product_entity.sku = stock.sku',
94  []
95  )->joinInner(
96  ['parent_link' => $this->resourceConnection->getTableName('catalog_product_super_link')],
97  'parent_link.product_id = product_entity.entity_id',
98  []
99  )->joinInner(
100  ['parent_product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
101  'parent_product_entity.' . $linkField . ' = parent_link.parent_id',
102  []
103  )
104  ->group(['parent_product_entity.sku']);
105 
106  return $select;
107  }
108 }
__construct(ResourceConnection $resourceConnection, IndexNameBuilder $indexNameBuilder, IndexNameResolverInterface $indexNameResolver, MetadataPool $metadataPool)
$connection
Definition: bulk.php:13