Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Row.php
Go to the documentation of this file.
1 <?php
7 
12 
18 class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
19 {
23  protected $flatItemWriter;
24 
28  protected $flatItemEraser;
29 
33  private $metadataPool;
34 
46  public function __construct(
49  \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper,
50  \Magento\Catalog\Model\Product\Type $productType,
51  TableBuilder $tableBuilder,
52  FlatTableBuilder $flatTableBuilder,
55  MetadataPool $metadataPool = null
56  ) {
57  parent::__construct(
58  $resource,
60  $productHelper,
61  $productType,
62  $tableBuilder,
63  $flatTableBuilder
64  );
65  $this->flatItemWriter = $flatItemWriter;
66  $this->flatItemEraser = $flatItemEraser;
67  $this->metadataPool = $metadataPool ?:
68  \Magento\Framework\App\ObjectManager::getInstance()->get(MetadataPool::class);
69  }
70 
78  public function execute($id = null)
79  {
80  if (!isset($id) || empty($id)) {
81  throw new \Magento\Framework\Exception\LocalizedException(
82  __('We can\'t rebuild the index for an undefined product.')
83  );
84  }
85  $ids = [$id];
86  $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
87 
88  $stores = $this->_storeManager->getStores();
89  foreach ($stores as $store) {
90  $tableExists = $this->_isFlatTableExists($store->getId());
91  if ($tableExists) {
92  $this->flatItemEraser->removeDeletedProducts($ids, $store->getId());
93  }
94 
95  /* @var $status \Magento\Eav\Model\Entity\Attribute */
96  $status = $this->_productIndexerHelper->getAttribute(ProductInterface::STATUS);
97  $statusTable = $status->getBackend()->getTable();
98  $statusConditions = [
99  'store_id IN(0,' . (int)$store->getId() . ')',
100  'attribute_id = ' . (int)$status->getId(),
101  $linkField . ' = ' . (int)$id,
102  ];
103  $select = $this->_connection->select();
104  $select->from($statusTable, ['value'])
105  ->where(implode(' AND ', $statusConditions))
106  ->order('store_id DESC')
107  ->limit(1);
108  $result = $this->_connection->query($select);
109  $status = $result->fetchColumn(0);
110 
111  if ($status == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) {
112  if (!$tableExists) {
113  $this->_flatTableBuilder->build(
114  $store->getId(),
115  $ids,
118  false
119  );
120  }
121  $this->flatItemWriter->write($store->getId(), $id, $this->_valueFieldSuffix);
122  } else {
123  $this->flatItemEraser->deleteProductsFromStore($id, $store->getId());
124  }
125  }
126 
127  return $this;
128  }
129 }
return false
Definition: gallery.phtml:36
$id
Definition: fieldset.phtml:14
$storeManager
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\App\ResourceConnection $resource, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper, \Magento\Catalog\Model\Product\Type $productType, TableBuilder $tableBuilder, FlatTableBuilder $flatTableBuilder, Indexer $flatItemWriter, Eraser $flatItemEraser, MetadataPool $metadataPool=null)
Definition: Row.php:46
$status
Definition: order_status.php:8