BundleOptionStockDataSelectBuilder.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model\ResourceModel\Indexer;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\Framework\DB\Select;
  9. /**
  10. * Class BundleOptionStockDataSelectBuilder
  11. * Is used to create Select object that is used for Bundle product stock status indexation
  12. *
  13. * @see \Magento\Bundle\Model\ResourceModel\Indexer\Stock::_prepareBundleOptionStockData
  14. */
  15. class BundleOptionStockDataSelectBuilder
  16. {
  17. /**
  18. * @param \Magento\Framework\App\ResourceConnection $resourceConnection
  19. * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
  20. */
  21. public function __construct(
  22. \Magento\Framework\App\ResourceConnection $resourceConnection,
  23. \Magento\Framework\EntityManager\MetadataPool $metadataPool
  24. ) {
  25. $this->resourceConnection = $resourceConnection;
  26. $this->metadataPool = $metadataPool;
  27. }
  28. /**
  29. * @param string $idxTable
  30. * @return Select
  31. */
  32. public function buildSelect($idxTable)
  33. {
  34. $select = $this->resourceConnection->getConnection()->select();
  35. $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
  36. $select->from(
  37. ['product' => $this->resourceConnection->getTableName('catalog_product_entity')],
  38. ['entity_id']
  39. )->join(
  40. ['bo' => $this->resourceConnection->getTableName('catalog_product_bundle_option')],
  41. "bo.parent_id = product.$linkField",
  42. []
  43. )->join(
  44. ['cis' => $this->resourceConnection->getTableName('cataloginventory_stock')],
  45. '',
  46. ['website_id', 'stock_id']
  47. )->joinLeft(
  48. ['bs' => $this->resourceConnection->getTableName('catalog_product_bundle_selection')],
  49. 'bs.option_id = bo.option_id',
  50. []
  51. )->joinLeft(
  52. ['i' => $idxTable],
  53. 'i.product_id = bs.product_id AND i.website_id = cis.website_id AND i.stock_id = cis.stock_id',
  54. []
  55. )->joinLeft(
  56. ['e' => $this->resourceConnection->getTableName('catalog_product_entity')],
  57. 'e.entity_id = bs.product_id',
  58. []
  59. )->group(
  60. ['product.entity_id', 'cis.website_id', 'cis.stock_id', 'bo.option_id']
  61. )->columns(['option_id' => 'bo.option_id']);
  62. return $select;
  63. }
  64. }