Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance - Optimize getSpecificOptions triggered by all getAttributeText calls #39476

Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 63 additions & 10 deletions app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2017 Adobe
* All Rights Reserved.
*/
namespace Magento\Eav\Model\Entity\Attribute\Source;

Expand Down Expand Up @@ -34,6 +34,13 @@ class Table extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource im
*/
protected $_attrOptionFactory;

/**
* Specific Options array memoization
*
* @var array
*/
protected $_specificOptions = null;

/**
* @var StoreManagerInterface
*/
Expand Down Expand Up @@ -100,24 +107,70 @@ public function getAllOptions($withEmpty = true, $defaultValues = false)
* Retrieve Option values array by ids
*
* @param string|array $ids
* @param bool $withEmpty Add empty option to array
* @param bool $withEmpty Add empty option to array
*
* @return array
*/
public function getSpecificOptions($ids, $withEmpty = true)
{
$options = $this->_attrOptionCollectionFactory->create()
->setPositionOrder('asc')
->setAttributeFilter($this->getAttribute()->getId())
->addFieldToFilter('main_table.option_id', ['in' => $ids])
->setStoreFilter($this->getAttribute()->getStoreId())
->load()
->toOptionArray();
$storeId = $this->getAttribute()->getStoreId();
$attributeId = $this->getAttribute()->getId();
$optionsIdsToLoad = [];
$options = [];

$optionIds = is_array($ids) ? $ids : [$ids];

foreach ($optionIds as $optionId) {
if (empty($this->_specificOptions[$storeId][$attributeId][$optionId])) {
$optionsIdsToLoad[] = $optionId;
} else {
$options[] = $this->_specificOptions[$storeId][$attributeId][$optionId];
}
}

if (!empty($optionsIdsToLoad)) {
$loadedOptions = $this->loadSpecificOptions(
$attributeId,
$optionsIdsToLoad,
$storeId
);

array_walk($loadedOptions, function ($loadedOption) use ($storeId, $attributeId, &$options) {
$this->_specificOptions[$storeId][$attributeId][$loadedOption['value']] = $loadedOption;
$options[] = $loadedOption;
});
}

if ($withEmpty) {
$options = $this->addEmptyOption($options);
}

return $options;
}

/**
* Load specific option in DB
*
* @param mixed $attributeId
* @param array $optionsIdsToLoad
* @param int $storeId
*
* @return array
*/
protected function loadSpecificOptions(mixed $attributeId, array $optionsIdsToLoad, int $storeId): array
{
return $this->_attrOptionCollectionFactory->create()
->setPositionOrder('asc')
->setAttributeFilter($attributeId)
->addFieldToFilter(
'main_table.option_id',
['in' => (count($optionsIdsToLoad)>1)?$optionsIdsToLoad:$optionsIdsToLoad[0]]
)
->setStoreFilter($storeId)
->load()
->toOptionArray();
}

/**
* Add an empty option to the array
*
Expand Down