Skip to content

Commit ab27445

Browse files
committed
MAGETWO-95827: Changing Attribute Set may lead to exception "Attempt to load value of nonexistent EAV attribute"
- Add sorting for union
1 parent 34345a1 commit ab27445

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use Magento\Eav\Model\Config;
99
use Magento\Framework\DataObject;
10-
use Magento\Framework\DB\Adapter\AdapterInterface;
1110
use Magento\Framework\DB\Select;
1211
use Magento\Framework\DB\Sql\UnionExpression;
1312
use Magento\Framework\EntityManager\MetadataPool;
@@ -138,39 +137,73 @@ public function execute($entityType, $entityData, $arguments = [])
138137
}
139138
}
140139
if (count($attributeTables)) {
140+
$identifiers = null;
141141
foreach ($attributeTables as $attributeTable => $attributeIds) {
142142
$select = $connection->select()
143143
->from(
144144
['t' => $attributeTable],
145-
['value' => 't.value', 'attribute_id' => 't.attribute_id', 'store_id' => 't.store_id']
145+
['value' => 't.value', 'attribute_id' => 't.attribute_id']
146146
)
147147
->where($metadata->getLinkField() . ' = ?', $entityData[$metadata->getLinkField()])
148148
->where('attribute_id IN (?)', $attributeIds);
149+
$attributeIdentifiers = [];
149150
foreach ($context as $scope) {
150151
//TODO: if (in table exists context field)
151152
$select->where(
152153
$connection->quoteIdentifier($scope->getIdentifier()) . ' IN (?)',
153154
$this->getContextVariables($scope)
154155
);
156+
$attributeIdentifiers[] = $scope->getIdentifier();
155157
}
158+
$attributeIdentifiers = array_unique($attributeIdentifiers);
159+
$identifiers = array_intersect($identifiers ?? $attributeIdentifiers, $attributeIdentifiers);
156160
$selects[] = $select;
157161
}
162+
$this->applyIdentifierForSelects($selects, $identifiers);
158163
$unionSelect = new UnionExpression($selects, Select::SQL_UNION_ALL, '( %s )');
159164
$orderedUnionSelect = $connection->select();
160165
$orderedUnionSelect->from(['u' => $unionSelect]);
161-
$orderedUnionSelect->order('store_id');
166+
$this->applyIdentifierForUnion($orderedUnionSelect, $identifiers);
162167
$attributes = $connection->fetchAll($orderedUnionSelect);
163168
foreach ($attributes as $attributeValue) {
164169
if (isset($attributesMap[$attributeValue['attribute_id']])) {
165170
$entityData[$attributesMap[$attributeValue['attribute_id']]] = $attributeValue['value'];
166171
} else {
167172
$this->logger->warning(
168-
"Attempt to load value of nonexistent EAV attribute '{$attributeValue['attribute_id']}'
173+
"Attempt to load value of nonexistent EAV attribute '{$attributeValue['attribute_id']}'
169174
for entity type '$entityType'."
170175
);
171176
}
172177
}
173178
}
174179
return $entityData;
175180
}
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+
}
176209
}

0 commit comments

Comments
 (0)