Skip to content

Commit 42e9d0b

Browse files
committed
Merge remote-tracking branch 'origin/MC-33288' into 2.4-develop-pr101
2 parents 485c714 + 2be562a commit 42e9d0b

File tree

2 files changed

+135
-1
lines changed
  • app/code/Magento/Catalog/Model/ResourceModel/Product
  • dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/ResourceModel/Product

2 files changed

+135
-1
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Relation.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,38 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
10+
use Magento\Framework\Model\ResourceModel\Db\Context;
11+
use Magento\Framework\EntityManager\MetadataPool;
12+
use Magento\Catalog\Api\Data\ProductInterface;
13+
814
/**
915
* Catalog Product Relations Resource model
1016
*
1117
* @author Magento Core Team <core@magentocommerce.com>
1218
*/
13-
class Relation extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
19+
class Relation extends AbstractDb
1420
{
21+
/**
22+
* @var MetadataPool
23+
*/
24+
private $metadataPool;
25+
26+
/**
27+
* @param Context $context
28+
* @param string $connectionName
29+
* @param MetadataPool $metadataPool
30+
*/
31+
public function __construct(
32+
Context $context,
33+
$connectionName = null,
34+
MetadataPool $metadataPool = null
35+
) {
36+
parent::__construct($context, $connectionName);
37+
$this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class);
38+
}
39+
1540
/**
1641
* Initialize resource model and define main table
1742
*
@@ -109,4 +134,27 @@ public function removeRelations($parentId, $childIds)
109134
}
110135
return $this;
111136
}
137+
138+
/**
139+
* Finds parent relations by given children ids.
140+
*
141+
* @param array $childrenIds Child products entity ids.
142+
* @return array Parent products entity ids.
143+
*/
144+
public function getRelationsByChildren(array $childrenIds): array
145+
{
146+
$connection = $this->getConnection();
147+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)
148+
->getLinkField();
149+
$select = $connection->select()
150+
->from(
151+
['cpe' => $this->getTable('catalog_product_entity')],
152+
'entity_id'
153+
)->join(
154+
['relation' => $this->getTable('catalog_product_relation')],
155+
'relation.parent_id = cpe.' . $linkField
156+
)->where('relation.child_id IN(?)', $childrenIds);
157+
158+
return $connection->fetchCol($select);
159+
}
112160
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Model\ResourceModel\Product;
7+
8+
use Magento\Catalog\Model\ResourceModel\Product\Relation;
9+
use Magento\Framework\ObjectManagerInterface;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Tests Catalog Product Relation resource model.
17+
*
18+
* @see Relation
19+
*/
20+
class RelationTest extends TestCase
21+
{
22+
/**
23+
* @var ObjectManagerInterface
24+
*/
25+
private $objectManager;
26+
27+
/**
28+
* @var Relation
29+
*/
30+
private $model;
31+
32+
/**
33+
* @var ProductRepositoryInterface
34+
*/
35+
private $productRepository;
36+
37+
/**
38+
* @var SearchCriteriaBuilder
39+
*/
40+
private $searchCriteriaBuilder;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp(): void
46+
{
47+
$this->objectManager = Bootstrap::getObjectManager();
48+
$this->model = $this->objectManager->get(Relation::class);
49+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
50+
$this->searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
51+
}
52+
53+
/**
54+
* Tests that getRelationsByChildren will return parent products entity ids of child products entity ids.
55+
*
56+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products.php
57+
*/
58+
public function testGetRelationsByChildren(): void
59+
{
60+
// Find configurable products options
61+
$productOptionSkus = ['simple_10', 'simple_20', 'simple_30', 'simple_40'];
62+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $productOptionSkus, 'in')
63+
->create();
64+
$productOptions = $this->productRepository->getList($searchCriteria)
65+
->getItems();
66+
67+
$productOptionsIds = [];
68+
69+
foreach ($productOptions as $productOption) {
70+
$productOptionsIds[] = $productOption->getId();
71+
}
72+
73+
// Find configurable products
74+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', ['configurable', 'configurable_12345'], 'in')
75+
->create();
76+
$configurableProducts = $this->productRepository->getList($searchCriteria)
77+
->getItems();
78+
79+
// Assert there are configurable products ids in result of getRelationsByChildren method.
80+
$result = $this->model->getRelationsByChildren($productOptionsIds);
81+
82+
foreach ($configurableProducts as $configurableProduct) {
83+
$this->assertContains($configurableProduct->getId(), $result);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)