Skip to content

Commit

Permalink
Merge pull request #1005 from romainruaud/fix_backward-compatibility-…
Browse files Browse the repository at this point in the history
…table-maintainer

Add proper exception handling when retrieving TableMaintainer class.
  • Loading branch information
romainruaud authored Jul 17, 2018
2 parents 4bb4af6 + 7dadada commit e911f87
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\ObjectManagerInterface;
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
use Magento\Framework\App\ResourceConnection;

/**
Expand Down Expand Up @@ -123,13 +122,7 @@ public function getRelationsByChild($childrenIds)
private function addIsVisibleInStoreFilter($select, $storeId)
{
$rootCategoryId = $this->getRootCategoryId($storeId);
$indexTableName = $this->getTable('catalog_category_product_index');

if ($tableMaintainer = $this->objectManager->get(TableMaintainer::class)) {
$indexTableName = $tableMaintainer->getMainTable($storeId);
}

$indexTable = $this->getTable($indexTableName);
$indexTable = $this->getCategoryProductIndexTable($storeId);

$visibilityJoinCond = $this->getConnection()->quoteInto(
'visibility.product_id = e.entity_id AND visibility.store_id = ?',
Expand All @@ -142,4 +135,27 @@ private function addIsVisibleInStoreFilter($select, $storeId)

return $this;
}

/**
* Retrieve category/product index table.
*
* @param int $storeId The store Id
*
* @return string
*/
private function getCategoryProductIndexTable($storeId)
{
// Init table name as legacy table name.
$indexTable = $this->getTable('catalog_category_product_index');

try {
// Retrieve table name for the current store Id from the TableMaintainer.
$tableMaintainer = $this->objectManager->get(\Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer::class);
$indexTable = $tableMaintainer->getMainTable($storeId);
} catch (\Exception $exception) {
// Occurs in Magento version where TableMaintainer is not implemented. Will default to legacy table.
}

return $indexTable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Eav\Indexer\Indexer;
use Magento\Framework\ObjectManagerInterface;
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;

/**
* Categories data datasource resource model.
Expand Down Expand Up @@ -173,10 +172,15 @@ protected function getEavConfig()
*/
protected function getCategoryProductIndexTable($storeId)
{
$indexTable = 'catalog_category_product_index';

if ($tableMaintainer = $this->objectManager->get(TableMaintainer::class)) {
$indexTable = $tableMaintainer->getMainTable($storeId);
// Init table name as legacy table name.
$indexTable = $this->getTable('catalog_category_product_index');

try {
// Retrieve table name for the current store Id from the TableMaintainer.
$tableMaintainer = $this->objectManager->get(\Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer::class);
$indexTable = $tableMaintainer->getMainTable($storeId);
} catch (\Exception $exception) {
// Occurs in Magento version where TableMaintainer is not implemented. Will default to legacy table.
}

return $indexTable;
Expand Down

0 comments on commit e911f87

Please sign in to comment.