Skip to content

Commit feccbc6

Browse files
committed
MAGETWO-54682: Fast load of product options
- MAGETWO-55756: Porting to 2.1
1 parent 4a4e5e3 commit feccbc6

File tree

4 files changed

+72
-24
lines changed

4 files changed

+72
-24
lines changed

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
namespace Magento\Catalog\Model\ResourceModel\Product;
1010

11+
use Magento\Catalog\Api\Data\ProductInterface;
1112
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
1213
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
1314
use Magento\Customer\Api\GroupManagementInterface;
1415
use Magento\Framework\DB\Select;
1516
use Magento\Framework\App\ObjectManager;
17+
use Magento\Framework\EntityManager\MetadataPool;
1618
use Magento\Store\Model\Store;
1719
use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler;
1820

@@ -252,6 +254,12 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
252254
* @var GalleryReadHandler
253255
*/
254256
private $productGalleryReadHandler;
257+
258+
/**
259+
* @var MetadataPool
260+
*/
261+
private $metadataPool;
262+
255263
/**
256264
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
257265
* @param \Psr\Log\LoggerInterface $logger
@@ -2092,15 +2100,11 @@ public function addTierPriceData()
20922100

20932101
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
20942102
$attribute = $this->getAttribute('tier_price');
2095-
if ($attribute->isScopeGlobal()) {
2096-
$websiteId = 0;
2097-
} else {
2098-
if (null !== $this->getStoreId()) {
2099-
$websiteId = $this->_storeManager->getStore($this->getStoreId())->getWebsiteId();
2100-
}
2103+
$websiteId = 0;
2104+
if (!$attribute->isScopeGlobal() && null !== $this->getStoreId()) {
2105+
$websiteId = $this->_storeManager->getStore($this->getStoreId())->getWebsiteId();
21012106
}
2102-
// var_dump($this->getStoreId());
2103-
// die;
2107+
21042108
$linkField = $this->getConnection()->getAutoIncrementField($this->getTable('catalog_product_entity'));
21052109
$connection = $this->getConnection();
21062110
$columns = [
@@ -2122,10 +2126,10 @@ public function addTierPriceData()
21222126
[$linkField, 'qty']
21232127
);
21242128

2125-
if ($websiteId == '0') {
2129+
if ($websiteId == 0) {
21262130
$select->where('website_id = ?', $websiteId);
21272131
} else {
2128-
$select->where('website_id IN(?)', ['0', $websiteId]);
2132+
$select->where('website_id IN(?)', [0, $websiteId]);
21292133
}
21302134

21312135
foreach ($connection->fetchAll($select) as $row) {
@@ -2191,7 +2195,6 @@ public function addMediaGalleryData()
21912195
return $this;
21922196
}
21932197

2194-
$mediaGalleries = [];
21952198
if (!$this->count()) {
21962199
return $this;
21972200
}
@@ -2202,9 +2205,12 @@ public function addMediaGalleryData()
22022205
$this->getStoreId(),
22032206
$attribute->getAttributeId()
22042207
);
2205-
2208+
2209+
$mediaGalleries = [];
2210+
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
2211+
22062212
foreach ($this->getConnection()->fetchAll($select) as $row) {
2207-
$mediaGalleries[$row['entity_id']][] = $row;
2213+
$mediaGalleries[$row[$linkField]][] = $row;
22082214
}
22092215

22102216
foreach ($this->getItems() as $item) {
@@ -2216,6 +2222,18 @@ public function addMediaGalleryData()
22162222
return $this;
22172223
}
22182224

2225+
/**
2226+
* Get MetadataPool instance
2227+
* @return MetadataPool
2228+
*/
2229+
private function getMetadataPool()
2230+
{
2231+
if (!$this->metadataPool) {
2232+
$this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
2233+
}
2234+
return $this->metadataPool;
2235+
}
2236+
22192237
/**
22202238
* Retrieve GalleryReadHandler
22212239
*

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product;
77

8+
use Magento\Catalog\Api\Data\ProductInterface;
89
use Magento\Catalog\Model\Product;
910
use Magento\Framework\DB\Select;
1011
use Magento\Store\Model\Store;
@@ -31,35 +32,44 @@ class LinkedProductSelectBuilderByBasePrice implements LinkedProductSelectBuilde
3132
*/
3233
private $catalogHelper;
3334

35+
/**
36+
* @var \Magento\Framework\EntityManager\MetadataPool
37+
*/
38+
private $metadataPool;
39+
3440
/**
3541
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3642
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
3743
* @param \Magento\Eav\Model\Config $eavConfig
3844
* @param \Magento\Catalog\Helper\Data $catalogHelper
45+
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
3946
*/
4047
public function __construct(
4148
\Magento\Store\Model\StoreManagerInterface $storeManager,
4249
\Magento\Framework\App\ResourceConnection $resourceConnection,
4350
\Magento\Eav\Model\Config $eavConfig,
44-
\Magento\Catalog\Helper\Data $catalogHelper
51+
\Magento\Catalog\Helper\Data $catalogHelper,
52+
\Magento\Framework\EntityManager\MetadataPool $metadataPool
4553
) {
4654
$this->storeManager = $storeManager;
4755
$this->resource = $resourceConnection;
4856
$this->eavConfig = $eavConfig;
4957
$this->catalogHelper = $catalogHelper;
58+
$this->metadataPool = $metadataPool;
5059
}
5160

5261
/**
5362
* {@inheritdoc}
5463
*/
5564
public function build($productId)
5665
{
66+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
5767
$priceAttribute = $this->eavConfig->getAttribute(Product::ENTITY, 'price');
5868
$priceSelect = $this->resource->getConnection()->select()
59-
->from(['t' => $priceAttribute->getBackendTable()], 'entity_id')
69+
->from(['t' => $priceAttribute->getBackendTable()], $linkField)
6070
->joinInner(
6171
['link' => $this->resource->getTableName('catalog_product_relation')],
62-
'link.child_id = t.entity_id',
72+
"link.child_id = t.$linkField",
6373
[]
6474
)->where('link.parent_id = ? ', $productId)
6575
->where('t.attribute_id = ?', $priceAttribute->getAttributeId())

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product;
77

8+
use Magento\Catalog\Api\Data\ProductInterface;
89
use Magento\Catalog\Model\Product;
910
use Magento\Framework\DB\Select;
1011
use Magento\Store\Model\Store;
@@ -41,35 +42,44 @@ class LinkedProductSelectBuilderBySpecialPrice implements LinkedProductSelectBui
4142
*/
4243
private $localeDate;
4344

45+
/**
46+
* @var \Magento\Framework\EntityManager\MetadataPool
47+
*/
48+
private $metadataPool;
49+
4450
/**
4551
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
4652
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
4753
* @param \Magento\Eav\Model\Config $eavConfig
4854
* @param \Magento\Catalog\Helper\Data $catalogHelper
4955
* @param \Magento\Framework\Stdlib\DateTime $dateTime
5056
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
57+
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
5158
*/
5259
public function __construct(
5360
\Magento\Store\Model\StoreManagerInterface $storeManager,
5461
\Magento\Framework\App\ResourceConnection $resourceConnection,
5562
\Magento\Eav\Model\Config $eavConfig,
5663
\Magento\Catalog\Helper\Data $catalogHelper,
5764
\Magento\Framework\Stdlib\DateTime $dateTime,
58-
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
65+
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
66+
\Magento\Framework\EntityManager\MetadataPool $metadataPool
5967
) {
6068
$this->storeManager = $storeManager;
6169
$this->resource = $resourceConnection;
6270
$this->eavConfig = $eavConfig;
6371
$this->catalogHelper = $catalogHelper;
6472
$this->dateTime = $dateTime;
6573
$this->localeDate = $localeDate;
74+
$this->metadataPool = $metadataPool;
6675
}
6776

6877
/**
6978
* {@inheritdoc}
7079
*/
7180
public function build($productId)
7281
{
82+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
7383
$connection = $this->resource->getConnection();
7484
$specialPriceAttribute = $this->eavConfig->getAttribute(Product::ENTITY, 'special_price');
7585
$specialPriceFromDate = $this->eavConfig->getAttribute(Product::ENTITY, 'special_from_date');
@@ -78,22 +88,22 @@ public function build($productId)
7888
$currentDate = $this->dateTime->formatDate($timestamp, false);
7989

8090
$specialPrice = $connection->select()
81-
->from(['t' => $specialPriceAttribute->getBackendTable()], 'entity_id')
91+
->from(['t' => $specialPriceAttribute->getBackendTable()], $linkField)
8292
->joinInner(
8393
['link' => $this->resource->getTableName('catalog_product_relation')],
84-
'link.child_id = t.entity_id',
94+
"link.child_id = t.{$linkField}",
8595
[]
8696
)->joinInner(
8797
['special_from' => $specialPriceFromDate->getBackendTable()],
8898
$connection->quoteInto(
89-
't.entity_id = special_from.entity_id AND special_from.attribute_id = ?',
99+
"t.{$linkField} = special_from.{$linkField} AND special_from.attribute_id = ?",
90100
$specialPriceFromDate->getAttributeId()
91101
),
92102
''
93103
)->joinInner(
94104
['special_to' => $specialPriceToDate->getBackendTable()],
95105
$connection->quoteInto(
96-
't.entity_id = special_to.entity_id AND special_to.attribute_id = ?',
106+
"t.{$linkField} = special_to.{$linkField} AND special_to.attribute_id = ?",
97107
$specialPriceToDate->getAttributeId()
98108
),
99109
''

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product;
77

8+
use Magento\Catalog\Api\Data\ProductInterface;
89
use Magento\Catalog\Model\Product;
910
use Magento\Framework\DB\Select;
1011

@@ -35,34 +36,43 @@ class LinkedProductSelectBuilderByTierPrice implements LinkedProductSelectBuilde
3536
*/
3637
private $catalogHelper;
3738

39+
/**
40+
* @var \Magento\Framework\EntityManager\MetadataPool
41+
*/
42+
private $metadataPool;
43+
3844
/**
3945
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
4046
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
4147
* @param \Magento\Customer\Model\Session $customerSession
4248
* @param \Magento\Catalog\Helper\Data $catalogHelper
49+
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
4350
*/
4451
public function __construct(
4552
\Magento\Store\Model\StoreManagerInterface $storeManager,
4653
\Magento\Framework\App\ResourceConnection $resourceConnection,
4754
\Magento\Customer\Model\Session $customerSession,
48-
\Magento\Catalog\Helper\Data $catalogHelper
55+
\Magento\Catalog\Helper\Data $catalogHelper,
56+
\Magento\Framework\EntityManager\MetadataPool $metadataPool
4957
) {
5058
$this->storeManager = $storeManager;
5159
$this->resource = $resourceConnection;
5260
$this->customerSession = $customerSession;
5361
$this->catalogHelper = $catalogHelper;
62+
$this->metadataPool = $metadataPool;
5463
}
5564

5665
/**
5766
* {@inheritdoc}
5867
*/
5968
public function build($productId)
6069
{
70+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
6171
$priceSelect = $this->resource->getConnection()->select()
62-
->from(['t' => $this->resource->getTableName('catalog_product_entity_tier_price')], 'entity_id')
72+
->from(['t' => $this->resource->getTableName('catalog_product_entity_tier_price')], $linkField)
6373
->joinInner(
6474
['link' => $this->resource->getTableName('catalog_product_relation')],
65-
'link.child_id = t.entity_id',
75+
"link.child_id = t.{$linkField}",
6676
[]
6777
)->where('link.parent_id = ? ', $productId)
6878
->where('t.all_groups = 1 OR customer_group_id = ?', $this->customerSession->getCustomerGroupId())

0 commit comments

Comments
 (0)