Skip to content

Commit e3cecaf

Browse files
committed
MAGETWO-54682: [Customer] Fast load of product options
1 parent f175469 commit e3cecaf

File tree

51 files changed

+1634
-278
lines changed

Some content is hidden

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

51 files changed

+1634
-278
lines changed

Diff for: app/code/Magento/Catalog/Model/Product/Gallery/ReadHandler.php

+45-15
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
{

Diff for: app/code/Magento/Catalog/Model/Product/Link.php

+1
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

Diff for: app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

+104-11
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
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;
15-
use Magento\Store\Model\Store;
16+
use Magento\Framework\App\ObjectManager;
1617
use Magento\Framework\EntityManager\MetadataPool;
17-
use Magento\Catalog\Api\Data\CategoryInterface;
18+
use Magento\Store\Model\Store;
19+
use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler;
1820

1921
/**
2022
* Product collection
@@ -232,7 +234,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
232234
protected $dateTime;
233235

234236
/**
235-
* @var GroupManagementInterface
237+
* @var \Magento\Customer\Api\GroupManagementInterface
236238
*/
237239
protected $_groupManagement;
238240

@@ -243,6 +245,21 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
243245
*/
244246
protected $needToAddWebsiteNamesToResult;
245247

248+
/**
249+
* @var Gallery
250+
*/
251+
private $mediaGalleryResource;
252+
253+
/**
254+
* @var GalleryReadHandler
255+
*/
256+
private $productGalleryReadHandler;
257+
258+
/**
259+
* @var MetadataPool
260+
*/
261+
private $metadataPool;
262+
246263
/**
247264
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
248265
* @param \Psr\Log\LoggerInterface $logger
@@ -2086,13 +2103,11 @@ public function addTierPriceData()
20862103

20872104
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
20882105
$attribute = $this->getAttribute('tier_price');
2089-
if ($attribute->isScopeGlobal()) {
2090-
$websiteId = 0;
2091-
} else {
2092-
if ($this->getStoreId()) {
2093-
$websiteId = $this->_storeManager->getStore($this->getStoreId())->getWebsiteId();
2094-
}
2106+
$websiteId = 0;
2107+
if (!$attribute->isScopeGlobal() && null !== $this->getStoreId()) {
2108+
$websiteId = $this->_storeManager->getStore($this->getStoreId())->getWebsiteId();
20952109
}
2110+
20962111
$linkField = $this->getConnection()->getAutoIncrementField($this->getTable('catalog_product_entity'));
20972112
$connection = $this->getConnection();
20982113
$columns = [
@@ -2114,10 +2129,10 @@ public function addTierPriceData()
21142129
[$linkField, 'qty']
21152130
);
21162131

2117-
if ($websiteId == '0') {
2132+
if ($websiteId == 0) {
21182133
$select->where('website_id = ?', $websiteId);
21192134
} else {
2120-
$select->where('website_id IN(?)', ['0', $websiteId]);
2135+
$select->where('website_id IN(?)', [0, $websiteId]);
21212136
}
21222137

21232138
foreach ($connection->fetchAll($select) as $row) {
@@ -2170,6 +2185,84 @@ public function addPriceDataFieldFilter($comparisonFormat, $fields)
21702185
return $this;
21712186
}
21722187

2188+
/**
2189+
* Add media gallery data to loaded items
2190+
*
2191+
* @return $this
2192+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2193+
* @SuppressWarnings(PHPMD.NPathComplexity)
2194+
*/
2195+
public function addMediaGalleryData()
2196+
{
2197+
if ($this->getFlag('media_gallery_added')) {
2198+
return $this;
2199+
}
2200+
2201+
if (!$this->count()) {
2202+
return $this;
2203+
}
2204+
2205+
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
2206+
$attribute = $this->getAttribute('media_gallery');
2207+
$select = $this->getMediaGalleryResource()->createBatchBaseSelect(
2208+
$this->getStoreId(),
2209+
$attribute->getAttributeId()
2210+
);
2211+
2212+
$mediaGalleries = [];
2213+
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
2214+
2215+
foreach ($this->getConnection()->fetchAll($select) as $row) {
2216+
$mediaGalleries[$row[$linkField]][] = $row;
2217+
}
2218+
2219+
foreach ($this->getItems() as $item) {
2220+
$mediaEntries = isset($mediaGalleries[$item->getId()]) ? $mediaGalleries[$item->getId()] : [];
2221+
$this->getGalleryReadHandler()->addMediaDataToProduct($item, $mediaEntries);
2222+
}
2223+
2224+
$this->setFlag('media_gallery_added', true);
2225+
return $this;
2226+
}
2227+
2228+
/**
2229+
* Get MetadataPool instance
2230+
* @return MetadataPool
2231+
*/
2232+
private function getMetadataPool()
2233+
{
2234+
if (!$this->metadataPool) {
2235+
$this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
2236+
}
2237+
return $this->metadataPool;
2238+
}
2239+
2240+
/**
2241+
* Retrieve GalleryReadHandler
2242+
*
2243+
* @return GalleryReadHandler
2244+
* @deprecated
2245+
*/
2246+
protected function getGalleryReadHandler()
2247+
{
2248+
if ($this->productGalleryReadHandler === null) {
2249+
$this->productGalleryReadHandler = ObjectManager::getInstance()->get(GalleryReadHandler::class);
2250+
}
2251+
return $this->productGalleryReadHandler;
2252+
}
2253+
2254+
/**
2255+
* @deprecated
2256+
* @return \Magento\Catalog\Model\ResourceModel\Product\Gallery
2257+
*/
2258+
private function getMediaGalleryResource()
2259+
{
2260+
if (null === $this->mediaGalleryResource) {
2261+
$this->mediaGalleryResource = ObjectManager::getInstance()->get(Gallery::class);
2262+
}
2263+
return $this->mediaGalleryResource;
2264+
}
2265+
21732266
/**
21742267
* Clear collection
21752268
*

Diff for: app/code/Magento/Catalog/Model/ResourceModel/Product/Gallery.php

+20-6
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
);

0 commit comments

Comments
 (0)