Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper exception handling when retrieving TableMaintainer class. #1005

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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