Skip to content

Commit e6d8d6e

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-95827-2' into EPAM-PR-41
2 parents f7256b0 + ab27445 commit e6d8d6e

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

app/code/Magento/Eav/Model/ResourceModel/ReadHandler.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
*/
66
namespace Magento\Eav\Model\ResourceModel;
77

8+
use Magento\Eav\Model\Config;
89
use Magento\Framework\DataObject;
10+
use Magento\Framework\DB\Select;
11+
use Magento\Framework\DB\Sql\UnionExpression;
912
use Magento\Framework\EntityManager\MetadataPool;
1013
use Magento\Framework\EntityManager\Operation\AttributeInterface;
1114
use Magento\Framework\Model\Entity\ScopeInterface;
1215
use Magento\Framework\Model\Entity\ScopeResolver;
1316
use Psr\Log\LoggerInterface;
1417

18+
/**
19+
* EAV read handler
20+
*/
1521
class ReadHandler implements AttributeInterface
1622
{
1723
/**
@@ -30,23 +36,21 @@ class ReadHandler implements AttributeInterface
3036
private $logger;
3137

3238
/**
33-
* @var \Magento\Eav\Model\Config
39+
* @var Config
3440
*/
3541
private $config;
3642

3743
/**
38-
* ReadHandler constructor.
39-
*
4044
* @param MetadataPool $metadataPool
4145
* @param ScopeResolver $scopeResolver
4246
* @param LoggerInterface $logger
43-
* @param \Magento\Eav\Model\Config $config
47+
* @param Config $config
4448
*/
4549
public function __construct(
4650
MetadataPool $metadataPool,
4751
ScopeResolver $scopeResolver,
4852
LoggerInterface $logger,
49-
\Magento\Eav\Model\Config $config
53+
Config $config
5054
) {
5155
$this->metadataPool = $metadataPool;
5256
$this->scopeResolver = $scopeResolver;
@@ -86,6 +90,8 @@ private function getEntityAttributes(string $entityType, DataObject $entity): ar
8690
}
8791

8892
/**
93+
* Get context variables
94+
*
8995
* @param ScopeInterface $scope
9096
* @return array
9197
*/
@@ -99,6 +105,8 @@ protected function getContextVariables(ScopeInterface $scope)
99105
}
100106

101107
/**
108+
* Execute read handler
109+
*
102110
* @param string $entityType
103111
* @param array $entityData
104112
* @param array $arguments
@@ -129,38 +137,73 @@ public function execute($entityType, $entityData, $arguments = [])
129137
}
130138
}
131139
if (count($attributeTables)) {
132-
$attributeTables = array_keys($attributeTables);
133-
foreach ($attributeTables as $attributeTable) {
140+
$identifiers = null;
141+
foreach ($attributeTables as $attributeTable => $attributeIds) {
134142
$select = $connection->select()
135143
->from(
136144
['t' => $attributeTable],
137145
['value' => 't.value', 'attribute_id' => 't.attribute_id']
138146
)
139-
->where($metadata->getLinkField() . ' = ?', $entityData[$metadata->getLinkField()]);
147+
->where($metadata->getLinkField() . ' = ?', $entityData[$metadata->getLinkField()])
148+
->where('attribute_id IN (?)', $attributeIds);
149+
$attributeIdentifiers = [];
140150
foreach ($context as $scope) {
141151
//TODO: if (in table exists context field)
142152
$select->where(
143-
$metadata->getEntityConnection()->quoteIdentifier($scope->getIdentifier()) . ' IN (?)',
153+
$connection->quoteIdentifier($scope->getIdentifier()) . ' IN (?)',
144154
$this->getContextVariables($scope)
145-
)->order('t.' . $scope->getIdentifier() . ' DESC');
155+
);
156+
$attributeIdentifiers[] = $scope->getIdentifier();
146157
}
158+
$attributeIdentifiers = array_unique($attributeIdentifiers);
159+
$identifiers = array_intersect($identifiers ?? $attributeIdentifiers, $attributeIdentifiers);
147160
$selects[] = $select;
148161
}
149-
$unionSelect = new \Magento\Framework\DB\Sql\UnionExpression(
150-
$selects,
151-
\Magento\Framework\DB\Select::SQL_UNION_ALL
152-
);
153-
foreach ($connection->fetchAll($unionSelect) as $attributeValue) {
162+
$this->applyIdentifierForSelects($selects, $identifiers);
163+
$unionSelect = new UnionExpression($selects, Select::SQL_UNION_ALL, '( %s )');
164+
$orderedUnionSelect = $connection->select();
165+
$orderedUnionSelect->from(['u' => $unionSelect]);
166+
$this->applyIdentifierForUnion($orderedUnionSelect, $identifiers);
167+
$attributes = $connection->fetchAll($orderedUnionSelect);
168+
foreach ($attributes as $attributeValue) {
154169
if (isset($attributesMap[$attributeValue['attribute_id']])) {
155170
$entityData[$attributesMap[$attributeValue['attribute_id']]] = $attributeValue['value'];
156171
} else {
157172
$this->logger->warning(
158-
"Attempt to load value of nonexistent EAV attribute '{$attributeValue['attribute_id']}'
173+
"Attempt to load value of nonexistent EAV attribute '{$attributeValue['attribute_id']}'
159174
for entity type '$entityType'."
160175
);
161176
}
162177
}
163178
}
164179
return $entityData;
165180
}
181+
182+
/**
183+
* Apply identifiers column on select array
184+
*
185+
* @param Select[] $selects
186+
* @param array $identifiers
187+
*/
188+
private function applyIdentifierForSelects(array $selects, array $identifiers)
189+
{
190+
foreach ($selects as $select) {
191+
foreach ($identifiers as $identifier) {
192+
$select->columns($identifier, 't');
193+
}
194+
}
195+
}
196+
197+
/**
198+
* Apply identifiers order on union select
199+
*
200+
* @param Select $unionSelect
201+
* @param array $identifiers
202+
*/
203+
private function applyIdentifierForUnion(Select $unionSelect, array $identifiers)
204+
{
205+
foreach ($identifiers as $identifier) {
206+
$unionSelect->order($identifier);
207+
}
208+
}
166209
}

app/code/Magento/Store/Test/Mftf/Section/StorefrontHeaderSection.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
1010
<section name="StorefrontHeaderSection">
1111
<element name="storeViewSwitcher" type="button" selector="#switcher-language-trigger"/>
12-
<element name="storeViewDropdown" type="button" selector="ul.switcher-dropdown"/>
12+
<element name="storeViewDropdown" type="button" selector=".active ul.switcher-dropdown"/>
1313
<element name="storeViewOption" type="button" selector="li.view-{{var1}}>a" parameterized="true"/>
1414
<element name="storeView" type="button" selector="//div[@class='actions dropdown options switcher-options active']//ul//li//a[contains(text(),'{{var}}')]" parameterized="true"/>
1515
</section>

lib/internal/Magento/Framework/DB/Sql/UnionExpression.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,25 @@ class UnionExpression extends Expression
2222
*/
2323
protected $type;
2424

25+
/**
26+
* @var string
27+
*/
28+
protected $pattern;
29+
2530
/**
2631
* @param Select[] $parts
27-
* @param string $type
32+
* @param string $type (optional)
33+
* @param string $pattern (optional)
2834
*/
29-
public function __construct(array $parts, $type = Select::SQL_UNION)
35+
public function __construct(array $parts, $type = Select::SQL_UNION, $pattern = '')
3036
{
3137
$this->parts = $parts;
3238
$this->type = $type;
39+
$this->pattern = $pattern;
3340
}
3441

3542
/**
36-
* @return string
43+
* @inheritdoc
3744
*/
3845
public function __toString()
3946
{
@@ -45,6 +52,10 @@ public function __toString()
4552
$parts[] = $part;
4653
}
4754
}
48-
return implode($parts, $this->type);
55+
$sql = implode($parts, $this->type);
56+
if ($this->pattern) {
57+
return sprintf($this->pattern, $sql);
58+
}
59+
return $sql;
4960
}
5061
}

0 commit comments

Comments
 (0)