From 81a3414da8203f1574c186a62041568184402aee Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Mon, 26 Oct 2015 17:15:03 +0200 Subject: [PATCH 1/2] MAGETWO-44237: CategoryLinkManagementInterface->getAssignedProducts() does not work with flat tables --- .../Catalog/Model/CategoryLinkManagement.php | 6 +++--- .../Catalog/Model/Resource/Category/Flat.php | 19 +++++++++++++++++++ .../Unit/Model/CategoryLinkManagementTest.php | 9 +++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php index 0c2c324bd8e2c..482192f8d1b3f 100644 --- a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php +++ b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php @@ -36,10 +36,10 @@ public function __construct( public function getAssignedProducts($categoryId) { $category = $this->categoryRepository->get($categoryId); - $productsPosition = $category->getProductsPosition(); - /** @var \Magento\Framework\Data\Collection\AbstractDb $products */ + /** @var \Magento\Catalog\Model\Resource\Product\Collection $products */ $products = $category->getProductCollection(); + $products->addFieldToSelect('position'); /** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface[] $links */ $links = []; @@ -49,7 +49,7 @@ public function getAssignedProducts($categoryId) /** @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/Resource/Category/Flat.php b/app/code/Magento/Catalog/Model/Resource/Category/Flat.php index fcc0fbf435f68..e50743dc1b985 100644 --- a/app/code/Magento/Catalog/Model/Resource/Category/Flat.php +++ b/app/code/Magento/Catalog/Model/Resource/Category/Flat.php @@ -671,4 +671,23 @@ public function getAnchorsAbove(array $filterIds, $storeId = 0) return $this->_getReadAdapter()->fetchCol($select); } + + /** + * Get positions of associated to category products + * + * @param \Magento\Catalog\Model\Category $category + * @return array + */ + public function getProductsPosition($category) + { + $select = $this->_getReadAdapter()->select()->from( + $this->getTable('catalog_category_product'), + ['product_id', 'position'] + )->where( + 'category_id = :category_id' + ); + $bind = ['category_id' => (int)$category->getId()]; + + return $this->_getReadAdapter()->fetchPairs($select, $bind); + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php b/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php index 9e75726f9dfa2..994bf9de2b062 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php @@ -44,7 +44,7 @@ public function testGetAssignedProducts() { $categoryId = 42; $productId = 55; - $productsPosition = [$productId => 25]; + $position = 25; $productSku = 'testSku'; $categoryProductLinkMock = $this->getMock('\Magento\Catalog\Api\Data\CategoryProductLinkInterface'); $categoryMock = $this->getMock( @@ -56,13 +56,14 @@ public function testGetAssignedProducts() ); $productMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false); $productMock->expects($this->once())->method('getSku')->willReturn($productSku); + $productMock->expects($this->once())->method('getData')->with('cat_index_position')->willReturn($position); $items = [$productId => $productMock]; - $productsMock = $this->getMock('\Magento\Framework\Data\Collection\AbstractDb', [], [], '', false); + $productsMock = $this->getMock('Magento\Catalog\Model\Resource\Product\Collection', [], [], '', false); $this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId) ->willReturn($categoryMock); - $categoryMock->expects($this->once())->method('getProductsPosition')->willReturn($productsPosition); $categoryMock->expects($this->once())->method('getProductCollection')->willReturn($productsMock); $categoryMock->expects($this->once())->method('getId')->willReturn($categoryId); + $productsMock->expects($this->once())->method('addFieldToSelect')->with('position')->willReturnSelf(); $productsMock->expects($this->once())->method('getItems')->willReturn($items); $this->productLinkFactoryMock->expects($this->once())->method('create')->willReturn($categoryProductLinkMock); $categoryProductLinkMock->expects($this->once()) @@ -71,7 +72,7 @@ public function testGetAssignedProducts() ->willReturnSelf(); $categoryProductLinkMock->expects($this->once()) ->method('setPosition') - ->with(25) + ->with($position) ->willReturnSelf(); $categoryProductLinkMock->expects($this->once()) ->method('setCategoryId') From 6855934cd8ef542220ac5b5cb41bf45f360c77a9 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Tue, 27 Oct 2015 14:25:31 +0200 Subject: [PATCH 2/2] MAGETWO-44237: CategoryLinkManagementInterface->getAssignedProducts() does not work with flat tables --- app/code/Magento/Catalog/Model/CategoryLinkManagement.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php index 482192f8d1b3f..d9c0f2798f732 100644 --- a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php +++ b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php @@ -45,7 +45,7 @@ public function getAssignedProducts($categoryId) $links = []; /** @var \Magento\Catalog\Model\Product $product */ - foreach ($products->getItems() as $productId => $product) { + foreach ($products->getItems() as $product) { /** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface $link */ $link = $this->productLinkFactory->create(); $link->setSku($product->getSku())