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

CategoryService - Reduce select attributes to improve query performance #849

Merged
merged 3 commits into from
Apr 10, 2024
Merged
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
12 changes: 3 additions & 9 deletions Model/Service/Product/Category/DefaultCategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@
namespace Nosto\Tagging\Model\Service\Product\Category;

use Exception;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Framework\Event\ManagerInterface;
use Magento\Store\Api\Data\StoreInterface;
use Nosto\Tagging\Logger\Logger as NostoLogger;
Expand All @@ -50,24 +48,20 @@ class DefaultCategoryService implements CategoryServiceInterface
{

private NostoLogger $logger;
private CategoryRepositoryInterface $categoryRepository;
private CollectionFactory $categoryCollectionFactory;
private ManagerInterface $eventManager;

/**
* Builder constructor.
* @param CategoryRepositoryInterface $categoryRepository
* @param CollectionFactory $categoryCollectionFactory
* @param NostoLogger $logger
* @param ManagerInterface $eventManager
*/
public function __construct(
CategoryRepositoryInterface $categoryRepository,
CollectionFactory $categoryCollectionFactory,
NostoLogger $logger,
ManagerInterface $eventManager
) {
$this->categoryRepository = $categoryRepository;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->logger = $logger;
$this->eventManager = $eventManager;
Expand Down Expand Up @@ -111,10 +105,11 @@ public function getCategory(Category $category, StoreInterface $store)
}

$categories = $this->categoryCollectionFactory->create()
->addAttributeToSelect('*')
->addAttributeToSelect('name')
->addAttributeToSelect('level')
->addAttributeToFilter('entity_id', $categoryIds)
->setStore($store->getId())
->addAttributeToSort('level', Collection::SORT_ORDER_ASC);
->addAttributeToSort('level');
foreach ($categories as $cat) {
if ($cat instanceof Category
&& $cat->getLevel() > 1
Expand All @@ -135,7 +130,6 @@ public function getCategory(Category $category, StoreInterface $store)
['categoryString' => $nostoCategory, 'magentoCategory' => $category]
);
}

return $nostoCategory;
}

Expand Down
Loading