Skip to content

Commit df20093

Browse files
committed
MAGETWO-54682: Fast load of product options
- MAGETWO-55756: Porting to 2.1
1 parent 0530a78 commit df20093

File tree

48 files changed

+1492
-223
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1492
-223
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/ReadHandler.php

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Catalog\Model\Product\Gallery;
77

88
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
9+
use Magento\Catalog\Model\Product;
910

1011
/**
1112
* Read handler for catalog product gallery.
@@ -40,7 +41,7 @@ public function __construct(
4041
}
4142

4243
/**
43-
* @param object $entity
44+
* @param Product $entity
4445
* @param array $arguments
4546
* @return object
4647
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
@@ -50,29 +51,57 @@ public function execute($entity, $arguments = [])
5051
$value = [];
5152
$value['images'] = [];
5253

53-
$localAttributes = ['label', 'position', 'disabled'];
54-
5554
$mediaEntries = $this->resourceModel->loadProductGalleryByAttributeId(
5655
$entity,
5756
$this->getAttribute()->getAttributeId()
5857
);
5958

60-
foreach ($mediaEntries as $mediaEntry) {
61-
foreach ($localAttributes as $localAttribute) {
62-
if ($mediaEntry[$localAttribute] === null) {
63-
$mediaEntry[$localAttribute] = $this->findDefaultValue($localAttribute, $mediaEntry);
64-
}
65-
}
59+
$this->addMediaDataToProduct(
60+
$entity,
61+
$mediaEntries
62+
);
63+
64+
return $entity;
65+
}
66+
67+
/**
68+
* @param Product $product
69+
* @param array $mediaEntries
70+
* @return void
71+
*/
72+
public function addMediaDataToProduct(Product $product, array $mediaEntries)
73+
{
74+
$attrCode = $this->getAttribute()->getAttributeCode();
75+
$value = [];
76+
$value['images'] = [];
77+
$value['values'] = [];
6678

67-
$value['images'][$mediaEntry['value_id']] = $mediaEntry;
79+
foreach ($mediaEntries as $mediaEntry) {
80+
$mediaEntry = $this->substituteNullsWithDefaultValues($mediaEntry);
81+
$value['images'][] = $mediaEntry;
6882
}
83+
$product->setData($attrCode, $value);
84+
}
6985

70-
$entity->setData(
71-
$this->getAttribute()->getAttributeCode(),
72-
$value
73-
);
86+
/**
87+
* @param array $rawData
88+
* @return array
89+
*/
90+
private function substituteNullsWithDefaultValues(array $rawData)
91+
{
92+
$processedData = [];
93+
foreach ($rawData as $key => $rawValue) {
94+
if (null !== $rawValue) {
95+
$processedValue = $rawValue;
96+
} elseif (isset($rawData[$key . '_default'])) {
97+
$processedValue = $rawData[$key . '_default'];
98+
} else {
99+
$processedValue = null;
100+
}
101+
$processedData[$key] = $processedValue;
102+
}
74103

75-
return $entity;
104+
return $processedData;
76105
}
77106

78107
/**
@@ -93,6 +122,7 @@ public function getAttribute()
93122
* @param string $key
94123
* @param string[] &$image
95124
* @return string
125+
* @deprecated
96126
*/
97127
protected function findDefaultValue($key, &$image)
98128
{

app/code/Magento/Catalog/Model/Product/Link.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Link extends \Magento\Framework\Model\AbstractModel
5757

5858
/**
5959
* @var \Magento\CatalogInventory\Helper\Stock
60+
* @deprecated
6061
*/
6162
protected $stockHelper;
6263

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

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
1313
use Magento\Customer\Api\GroupManagementInterface;
1414
use Magento\Framework\DB\Select;
15+
use Magento\Framework\App\ObjectManager;
1516
use Magento\Store\Model\Store;
16-
use Magento\Framework\EntityManager\MetadataPool;
17-
use Magento\Catalog\Api\Data\CategoryInterface;
17+
use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler;
1818

1919
/**
2020
* Product collection
@@ -232,7 +232,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
232232
protected $dateTime;
233233

234234
/**
235-
* @var GroupManagementInterface
235+
* @var \Magento\Customer\Api\GroupManagementInterface
236236
*/
237237
protected $_groupManagement;
238238

@@ -243,6 +243,15 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
243243
*/
244244
protected $needToAddWebsiteNamesToResult;
245245

246+
/**
247+
* @var Gallery
248+
*/
249+
private $mediaGalleryResource;
250+
251+
/**
252+
* @var GalleryReadHandler
253+
*/
254+
private $productGalleryReadHandler;
246255
/**
247256
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
248257
* @param \Psr\Log\LoggerInterface $logger
@@ -2086,10 +2095,12 @@ public function addTierPriceData()
20862095
if ($attribute->isScopeGlobal()) {
20872096
$websiteId = 0;
20882097
} else {
2089-
if ($this->getStoreId()) {
2098+
if (null !== $this->getStoreId()) {
20902099
$websiteId = $this->_storeManager->getStore($this->getStoreId())->getWebsiteId();
20912100
}
20922101
}
2102+
// var_dump($this->getStoreId());
2103+
// die;
20932104
$linkField = $this->getConnection()->getAutoIncrementField($this->getTable('catalog_product_entity'));
20942105
$connection = $this->getConnection();
20952106
$columns = [
@@ -2167,6 +2178,70 @@ public function addPriceDataFieldFilter($comparisonFormat, $fields)
21672178
return $this;
21682179
}
21692180

2181+
/**
2182+
* Add media gallery data to loaded items
2183+
*
2184+
* @return $this
2185+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2186+
* @SuppressWarnings(PHPMD.NPathComplexity)
2187+
*/
2188+
public function addMediaGalleryData()
2189+
{
2190+
if ($this->getFlag('media_gallery_added')) {
2191+
return $this;
2192+
}
2193+
2194+
$mediaGalleries = [];
2195+
if (!$this->count()) {
2196+
return $this;
2197+
}
2198+
2199+
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
2200+
$attribute = $this->getAttribute('media_gallery');
2201+
$select = $this->getMediaGalleryResource()->createBatchBaseSelect(
2202+
$this->getStoreId(),
2203+
$attribute->getAttributeId()
2204+
);
2205+
2206+
foreach ($this->getConnection()->fetchAll($select) as $row) {
2207+
$mediaGalleries[$row['entity_id']][] = $row;
2208+
}
2209+
2210+
foreach ($this->getItems() as $item) {
2211+
$mediaEntries = isset($mediaGalleries[$item->getId()]) ? $mediaGalleries[$item->getId()] : [];
2212+
$this->getGalleryReadHandler()->addMediaDataToProduct($item, $mediaEntries);
2213+
}
2214+
2215+
$this->setFlag('media_gallery_added', true);
2216+
return $this;
2217+
}
2218+
2219+
/**
2220+
* Retrieve GalleryReadHandler
2221+
*
2222+
* @return GalleryReadHandler
2223+
* @deprecated
2224+
*/
2225+
protected function getGalleryReadHandler()
2226+
{
2227+
if ($this->productGalleryReadHandler === null) {
2228+
$this->productGalleryReadHandler = ObjectManager::getInstance()->get(GalleryReadHandler::class);
2229+
}
2230+
return $this->productGalleryReadHandler;
2231+
}
2232+
2233+
/**
2234+
* @deprecated
2235+
* @return \Magento\Catalog\Model\ResourceModel\Product\Gallery
2236+
*/
2237+
private function getMediaGalleryResource()
2238+
{
2239+
if (null === $this->mediaGalleryResource) {
2240+
$this->mediaGalleryResource = ObjectManager::getInstance()->get(Gallery::class);
2241+
}
2242+
return $this->mediaGalleryResource;
2243+
}
2244+
21702245
/**
21712246
* Clear collection
21722247
*

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

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

8+
use Magento\Store\Model\Store;
9+
810
/**
911
* Catalog product media gallery resource model.
1012
*/
@@ -129,6 +131,23 @@ public function loadProductGalleryByAttributeId($product, $attributeId)
129131
* @throws \Magento\Framework\Exception\LocalizedException
130132
*/
131133
protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
134+
{
135+
$select = $this->createBatchBaseSelect($storeId, $attributeId);
136+
137+
$select = $select->where(
138+
'entity.' . $this->metadata->getLinkField() .' = ?',
139+
$entityId
140+
);
141+
return $select;
142+
}
143+
144+
/**
145+
* @param int $storeId
146+
* @param int $attributeId
147+
* @return \Magento\Framework\DB\Select
148+
* @throws \Magento\Framework\Exception\LocalizedException
149+
*/
150+
public function createBatchBaseSelect($storeId, $attributeId)
132151
{
133152
$linkField = $this->metadata->getLinkField();
134153

@@ -158,7 +177,6 @@ protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
158177
[
159178
$mainTableAlias . '.value_id = value.value_id',
160179
$this->getConnection()->quoteInto('value.store_id = ?', (int)$storeId),
161-
$this->getConnection()->quoteInto('value.' . $linkField . ' = ?', (int)$entityId)
162180
]
163181
),
164182
['label', 'position', 'disabled']
@@ -168,8 +186,7 @@ protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
168186
' AND ',
169187
[
170188
$mainTableAlias . '.value_id = default_value.value_id',
171-
'default_value.store_id = 0',
172-
$this->getConnection()->quoteInto('default_value.' . $linkField . ' = ?', (int)$entityId)
189+
$this->getConnection()->quoteInto('default_value.store_id = ?', Store::DEFAULT_STORE_ID),
173190
]
174191
),
175192
['label_default' => 'label', 'position_default' => 'position', 'disabled_default' => 'disabled']
@@ -178,9 +195,6 @@ protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
178195
$attributeId
179196
)->where(
180197
$mainTableAlias . '.disabled = 0'
181-
)->where(
182-
'entity.' . $linkField . ' = ?',
183-
$entityId
184198
)->order(
185199
$positionCheckSql . ' ' . \Magento\Framework\DB\Select::SQL_ASC
186200
);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer;
7+
8+
use Magento\Catalog\Model\Product;
9+
use Magento\Framework\DB\Select;
10+
use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;
11+
12+
class LinkedProductSelectBuilderByIndexPrice implements LinkedProductSelectBuilderInterface
13+
{
14+
/**
15+
* @var \Magento\Store\Model\StoreManagerInterface
16+
*/
17+
private $storeManager;
18+
19+
/**
20+
* @var \Magento\Framework\App\ResourceConnection
21+
*/
22+
private $resource;
23+
24+
/**
25+
* @var \Magento\Customer\Model\Session
26+
*/
27+
private $customerSession;
28+
29+
/**
30+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
31+
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
32+
* @param \Magento\Customer\Model\Session $customerSession
33+
*/
34+
public function __construct(
35+
\Magento\Store\Model\StoreManagerInterface $storeManager,
36+
\Magento\Framework\App\ResourceConnection $resourceConnection,
37+
\Magento\Customer\Model\Session $customerSession
38+
) {
39+
$this->storeManager = $storeManager;
40+
$this->resource = $resourceConnection;
41+
$this->customerSession = $customerSession;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function build($productId)
48+
{
49+
return [$this->resource->getConnection()->select()
50+
->from(['t' => $this->resource->getTableName('catalog_product_index_price')], 'entity_id')
51+
->joinInner(
52+
['link' => $this->resource->getTableName('catalog_product_relation')],
53+
'link.child_id = t.entity_id',
54+
[]
55+
)->where('link.parent_id = ? ', $productId)
56+
->where('t.website_id = ?', $this->storeManager->getStore()->getWebsiteId())
57+
->where('t.customer_group_id = ?', $this->customerSession->getCustomerGroupId())
58+
->order('t.min_price ' . Select::SQL_ASC)
59+
->limit(1)];
60+
}
61+
}

0 commit comments

Comments
 (0)