Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockStatusSelectBuilder.php
Go to the documentation of this file.
1 <?php
8 
12 
20 {
21 
27  public function __construct(
29  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
30  \Magento\Eav\Model\Config $eavConfig
31  ) {
32  $this->resourceConnection = $resourceConnection;
33  $this->metadataPool = $metadataPool;
34  $this->eavConfig = $eavConfig;
35  }
36 
42  public function buildSelect(Select $select)
43  {
44  $select = clone $select;
45  $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
46  $linkField = $metadata->getLinkField();
47 
48  $select->reset(
50  )->columns(
51  ['e.entity_id', 'cis.website_id', 'cis.stock_id']
52  )->joinLeft(
53  ['o' => $this->resourceConnection->getTableName('catalog_product_bundle_stock_index')],
54  'o.entity_id = e.entity_id AND o.website_id = cis.website_id AND o.stock_id = cis.stock_id',
55  []
56  )->joinInner(
57  ['cpr' => $this->resourceConnection->getTableName('catalog_product_relation')],
58  'e.' . $linkField . ' = cpr.parent_id',
59  []
60  )->columns(
61  ['qty' => new \Zend_Db_Expr('0')]
62  );
63 
64  if ($metadata->getIdentifierField() === $metadata->getLinkField()) {
65  $select->joinInner(
66  ['cpei' => $this->resourceConnection->getTableName('catalog_product_entity_int')],
67  'cpr.child_id = cpei.' . $linkField
68  . ' AND cpei.attribute_id = ' . $this->getAttribute('status')->getId()
69  . ' AND cpei.value = ' . ProductStatus::STATUS_ENABLED,
70  []
71  );
72  } else {
73  $select->joinInner(
74  ['cpel' => $this->resourceConnection->getTableName('catalog_product_entity')],
75  'cpel.entity_id = cpr.child_id',
76  []
77  )->joinInner(
78  ['cpei' => $this->resourceConnection->getTableName('catalog_product_entity_int')],
79  'cpel.'. $linkField . ' = cpei.' . $linkField
80  . ' AND cpei.attribute_id = ' . $this->getAttribute('status')->getId()
81  . ' AND cpei.value = ' . ProductStatus::STATUS_ENABLED,
82  []
83  );
84  }
85 
86  return $select;
87  }
88 
95  private function getAttribute($attributeCode)
96  {
97  return $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
98  }
99 }
__construct(\Magento\Framework\App\ResourceConnection $resourceConnection, \Magento\Framework\EntityManager\MetadataPool $metadataPool, \Magento\Eav\Model\Config $eavConfig)
const COLUMNS
Definition: Select.php:48
$attributeCode
Definition: extend.phtml:12