Skip to content

Commit

Permalink
MAGETWO-54682: [Customer] Fast load of product options
Browse files Browse the repository at this point in the history
  • Loading branch information
kandy committed Jul 29, 2016
1 parent f175469 commit e3cecaf
Show file tree
Hide file tree
Showing 51 changed files with 1,634 additions and 278 deletions.
60 changes: 45 additions & 15 deletions app/code/Magento/Catalog/Model/Product/Gallery/ReadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Catalog\Model\Product\Gallery;

use Magento\Framework\EntityManager\Operation\ExtensionInterface;
use Magento\Catalog\Model\Product;

/**
* Read handler for catalog product gallery.
Expand Down Expand Up @@ -40,7 +41,7 @@ public function __construct(
}

/**
* @param object $entity
* @param Product $entity
* @param array $arguments
* @return object
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
Expand All @@ -50,29 +51,57 @@ public function execute($entity, $arguments = [])
$value = [];
$value['images'] = [];

$localAttributes = ['label', 'position', 'disabled'];

$mediaEntries = $this->resourceModel->loadProductGalleryByAttributeId(
$entity,
$this->getAttribute()->getAttributeId()
);

foreach ($mediaEntries as $mediaEntry) {
foreach ($localAttributes as $localAttribute) {
if ($mediaEntry[$localAttribute] === null) {
$mediaEntry[$localAttribute] = $this->findDefaultValue($localAttribute, $mediaEntry);
}
}
$this->addMediaDataToProduct(
$entity,
$mediaEntries
);

return $entity;
}

/**
* @param Product $product
* @param array $mediaEntries
* @return void
*/
public function addMediaDataToProduct(Product $product, array $mediaEntries)
{
$attrCode = $this->getAttribute()->getAttributeCode();
$value = [];
$value['images'] = [];
$value['values'] = [];

$value['images'][$mediaEntry['value_id']] = $mediaEntry;
foreach ($mediaEntries as $mediaEntry) {
$mediaEntry = $this->substituteNullsWithDefaultValues($mediaEntry);
$value['images'][] = $mediaEntry;
}
$product->setData($attrCode, $value);
}

$entity->setData(
$this->getAttribute()->getAttributeCode(),
$value
);
/**
* @param array $rawData
* @return array
*/
private function substituteNullsWithDefaultValues(array $rawData)
{
$processedData = [];
foreach ($rawData as $key => $rawValue) {
if (null !== $rawValue) {
$processedValue = $rawValue;
} elseif (isset($rawData[$key . '_default'])) {
$processedValue = $rawData[$key . '_default'];
} else {
$processedValue = null;
}
$processedData[$key] = $processedValue;
}

return $entity;
return $processedData;
}

/**
Expand All @@ -93,6 +122,7 @@ public function getAttribute()
* @param string $key
* @param string[] &$image
* @return string
* @deprecated
*/
protected function findDefaultValue($key, &$image)
{
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/Model/Product/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Link extends \Magento\Framework\Model\AbstractModel

/**
* @var \Magento\CatalogInventory\Helper\Stock
* @deprecated
*/
protected $stockHelper;

Expand Down
115 changes: 104 additions & 11 deletions app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

namespace Magento\Catalog\Model\ResourceModel\Product;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\Customer\Api\GroupManagementInterface;
use Magento\Framework\DB\Select;
use Magento\Store\Model\Store;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Store\Model\Store;
use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler;

/**
* Product collection
Expand Down Expand Up @@ -232,7 +234,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
protected $dateTime;

/**
* @var GroupManagementInterface
* @var \Magento\Customer\Api\GroupManagementInterface
*/
protected $_groupManagement;

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

/**
* @var Gallery
*/
private $mediaGalleryResource;

/**
* @var GalleryReadHandler
*/
private $productGalleryReadHandler;

/**
* @var MetadataPool
*/
private $metadataPool;

/**
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
* @param \Psr\Log\LoggerInterface $logger
Expand Down Expand Up @@ -2086,13 +2103,11 @@ public function addTierPriceData()

/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$attribute = $this->getAttribute('tier_price');
if ($attribute->isScopeGlobal()) {
$websiteId = 0;
} else {
if ($this->getStoreId()) {
$websiteId = $this->_storeManager->getStore($this->getStoreId())->getWebsiteId();
}
$websiteId = 0;
if (!$attribute->isScopeGlobal() && null !== $this->getStoreId()) {
$websiteId = $this->_storeManager->getStore($this->getStoreId())->getWebsiteId();
}

$linkField = $this->getConnection()->getAutoIncrementField($this->getTable('catalog_product_entity'));
$connection = $this->getConnection();
$columns = [
Expand All @@ -2114,10 +2129,10 @@ public function addTierPriceData()
[$linkField, 'qty']
);

if ($websiteId == '0') {
if ($websiteId == 0) {
$select->where('website_id = ?', $websiteId);
} else {
$select->where('website_id IN(?)', ['0', $websiteId]);
$select->where('website_id IN(?)', [0, $websiteId]);
}

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

/**
* Add media gallery data to loaded items
*
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function addMediaGalleryData()
{
if ($this->getFlag('media_gallery_added')) {
return $this;
}

if (!$this->count()) {
return $this;
}

/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$attribute = $this->getAttribute('media_gallery');
$select = $this->getMediaGalleryResource()->createBatchBaseSelect(
$this->getStoreId(),
$attribute->getAttributeId()
);

$mediaGalleries = [];
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();

foreach ($this->getConnection()->fetchAll($select) as $row) {
$mediaGalleries[$row[$linkField]][] = $row;
}

foreach ($this->getItems() as $item) {
$mediaEntries = isset($mediaGalleries[$item->getId()]) ? $mediaGalleries[$item->getId()] : [];
$this->getGalleryReadHandler()->addMediaDataToProduct($item, $mediaEntries);
}

$this->setFlag('media_gallery_added', true);
return $this;
}

/**
* Get MetadataPool instance
* @return MetadataPool
*/
private function getMetadataPool()
{
if (!$this->metadataPool) {
$this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
}
return $this->metadataPool;
}

/**
* Retrieve GalleryReadHandler
*
* @return GalleryReadHandler
* @deprecated
*/
protected function getGalleryReadHandler()
{
if ($this->productGalleryReadHandler === null) {
$this->productGalleryReadHandler = ObjectManager::getInstance()->get(GalleryReadHandler::class);
}
return $this->productGalleryReadHandler;
}

/**
* @deprecated
* @return \Magento\Catalog\Model\ResourceModel\Product\Gallery
*/
private function getMediaGalleryResource()
{
if (null === $this->mediaGalleryResource) {
$this->mediaGalleryResource = ObjectManager::getInstance()->get(Gallery::class);
}
return $this->mediaGalleryResource;
}

/**
* Clear collection
*
Expand Down
26 changes: 20 additions & 6 deletions app/code/Magento/Catalog/Model/ResourceModel/Product/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Catalog\Model\ResourceModel\Product;

use Magento\Store\Model\Store;

/**
* Catalog product media gallery resource model.
*/
Expand Down Expand Up @@ -129,6 +131,23 @@ public function loadProductGalleryByAttributeId($product, $attributeId)
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
{
$select = $this->createBatchBaseSelect($storeId, $attributeId);

$select = $select->where(
'entity.' . $this->metadata->getLinkField() .' = ?',
$entityId
);
return $select;
}

/**
* @param int $storeId
* @param int $attributeId
* @return \Magento\Framework\DB\Select
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function createBatchBaseSelect($storeId, $attributeId)
{
$linkField = $this->metadata->getLinkField();

Expand Down Expand Up @@ -158,7 +177,6 @@ protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
[
$mainTableAlias . '.value_id = value.value_id',
$this->getConnection()->quoteInto('value.store_id = ?', (int)$storeId),
$this->getConnection()->quoteInto('value.' . $linkField . ' = ?', (int)$entityId)
]
),
['label', 'position', 'disabled']
Expand All @@ -168,8 +186,7 @@ protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
' AND ',
[
$mainTableAlias . '.value_id = default_value.value_id',
'default_value.store_id = 0',
$this->getConnection()->quoteInto('default_value.' . $linkField . ' = ?', (int)$entityId)
$this->getConnection()->quoteInto('default_value.store_id = ?', Store::DEFAULT_STORE_ID),
]
),
['label_default' => 'label', 'position_default' => 'position', 'disabled_default' => 'disabled']
Expand All @@ -178,9 +195,6 @@ protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
$attributeId
)->where(
$mainTableAlias . '.disabled = 0'
)->where(
'entity.' . $linkField . ' = ?',
$entityId
)->order(
$positionCheckSql . ' ' . \Magento\Framework\DB\Select::SQL_ASC
);
Expand Down
Loading

0 comments on commit e3cecaf

Please sign in to comment.