From aa711a7cbaf181148ab130cfde02298b76654c8c Mon Sep 17 00:00:00 2001 From: Valeriy Nayda <vnayda@ebay.com> Date: Sat, 17 Oct 2015 15:48:29 +0300 Subject: [PATCH] MAGETWO-44237: CategoryLinkManagementInterface->getAssignedProducts() does not work with flat tables --- .../Catalog/Model/CategoryLinkManagement.php | 6 +++--- .../Model/ResourceModel/Category/Flat.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php index 0c2c324bd8e..f0405be4621 100644 --- a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php +++ b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php @@ -36,10 +36,10 @@ class CategoryLinkManagement implements \Magento\Catalog\Api\CategoryLinkManagem public function getAssignedProducts($categoryId) { $category = $this->categoryRepository->get($categoryId); - $productsPosition = $category->getProductsPosition(); - /** @var \Magento\Framework\Data\Collection\AbstractDb $products */ + /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $products */ $products = $category->getProductCollection(); + $products->addFieldToSelect('position'); /** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface[] $links */ $links = []; @@ -49,7 +49,7 @@ class CategoryLinkManagement implements \Magento\Catalog\Api\CategoryLinkManagem /** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface $link */ $link = $this->productLinkFactory->create(); $link->setSku($product->getSku()) - ->setPosition($productsPosition[$productId]) + ->setPosition($product->getData('cat_index_position')) ->setCategoryId($category->getId()); $links[] = $link; } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php index 089bfbc10e0..7c054546878 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php @@ -671,4 +671,23 @@ class Flat extends \Magento\Indexer\Model\ResourceModel\AbstractResource return $this->getConnection()->fetchCol($select); } + + /** + * Get positions of associated to category products + * + * @param \Magento\Catalog\Model\Category $category + * @return array + */ + public function getProductsPosition($category) + { + $select = $this->getConnection()->select()->from( + $this->getTable('catalog_category_product'), + ['product_id', 'position'] + )->where( + 'category_id = :category_id' + ); + $bind = ['category_id' => (int)$category->getId()]; + + return $this->getConnection()->fetchPairs($select, $bind); + } } -- GitLab