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;
21 
23 {
27  private $resourceConnection;
28 
32  private $indexNameBuilder;
33 
37  private $indexNameResolver;
38 
42  private $metadataPool;
43 
50  public function __construct(
51  ResourceConnection $resourceConnection,
52  IndexNameBuilder $indexNameBuilder,
53  IndexNameResolverInterface $indexNameResolver,
54  MetadataPool $metadataPool
55  ) {
56  $this->resourceConnection = $resourceConnection;
57  $this->indexNameBuilder = $indexNameBuilder;
58  $this->indexNameResolver = $indexNameResolver;
59  $this->metadataPool = $metadataPool;
60  }
61 
69  public function execute(int $stockId): Select
70  {
71  $connection = $this->resourceConnection->getConnection();
72 
73  $indexName = $this->indexNameBuilder
74  ->setIndexId(InventoryIndexer::INDEXER_ID)
75  ->addDimension('stock_', (string)$stockId)
76  ->setAlias(Alias::ALIAS_MAIN)
77  ->build();
78 
79  $indexTableName = $this->indexNameResolver->resolveName($indexName);
80 
81  $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
82  $linkField = $metadata->getLinkField();
83 
84  $select = $connection->select()
85  ->from(
86  ['stock' => $indexTableName],
87  [
88  IndexStructure::SKU => 'parent_product_entity.sku',
89  IndexStructure::QUANTITY => 'SUM(stock.quantity)',
90  IndexStructure::IS_SALABLE => 'MAX(stock.is_salable)',
91  ]
92  )->joinInner(
93  ['product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
94  'product_entity.sku = stock.sku',
95  []
96  )->joinInner(
97  ['parent_link' => $this->resourceConnection->getTableName('catalog_product_link')],
98  'parent_link.linked_product_id = product_entity.entity_id
99  AND parent_link.link_type_id = ' . Link::LINK_TYPE_GROUPED,
100  []
101  )->joinInner(
102  ['parent_product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
103  'parent_product_entity.' . $linkField . ' = parent_link.product_id',
104  []
105  )
106  ->group(['parent_product_entity.sku']);
107 
108  return $select;
109  }
110 }
__construct(ResourceConnection $resourceConnection, IndexNameBuilder $indexNameBuilder, IndexNameResolverInterface $indexNameResolver, MetadataPool $metadataPool)
$connection
Definition: bulk.php:13