|
7 | 7 |
|
8 | 8 | use Magento\Eav\Model\Config;
|
9 | 9 | use Magento\Framework\DataObject;
|
10 |
| -use Magento\Framework\DB\Adapter\AdapterInterface; |
11 | 10 | use Magento\Framework\DB\Select;
|
12 | 11 | use Magento\Framework\DB\Sql\UnionExpression;
|
13 | 12 | use Magento\Framework\EntityManager\MetadataPool;
|
@@ -138,39 +137,73 @@ public function execute($entityType, $entityData, $arguments = [])
|
138 | 137 | }
|
139 | 138 | }
|
140 | 139 | if (count($attributeTables)) {
|
| 140 | + $identifiers = null; |
141 | 141 | foreach ($attributeTables as $attributeTable => $attributeIds) {
|
142 | 142 | $select = $connection->select()
|
143 | 143 | ->from(
|
144 | 144 | ['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'] |
146 | 146 | )
|
147 | 147 | ->where($metadata->getLinkField() . ' = ?', $entityData[$metadata->getLinkField()])
|
148 | 148 | ->where('attribute_id IN (?)', $attributeIds);
|
| 149 | + $attributeIdentifiers = []; |
149 | 150 | foreach ($context as $scope) {
|
150 | 151 | //TODO: if (in table exists context field)
|
151 | 152 | $select->where(
|
152 | 153 | $connection->quoteIdentifier($scope->getIdentifier()) . ' IN (?)',
|
153 | 154 | $this->getContextVariables($scope)
|
154 | 155 | );
|
| 156 | + $attributeIdentifiers[] = $scope->getIdentifier(); |
155 | 157 | }
|
| 158 | + $attributeIdentifiers = array_unique($attributeIdentifiers); |
| 159 | + $identifiers = array_intersect($identifiers ?? $attributeIdentifiers, $attributeIdentifiers); |
156 | 160 | $selects[] = $select;
|
157 | 161 | }
|
| 162 | + $this->applyIdentifierForSelects($selects, $identifiers); |
158 | 163 | $unionSelect = new UnionExpression($selects, Select::SQL_UNION_ALL, '( %s )');
|
159 | 164 | $orderedUnionSelect = $connection->select();
|
160 | 165 | $orderedUnionSelect->from(['u' => $unionSelect]);
|
161 |
| - $orderedUnionSelect->order('store_id'); |
| 166 | + $this->applyIdentifierForUnion($orderedUnionSelect, $identifiers); |
162 | 167 | $attributes = $connection->fetchAll($orderedUnionSelect);
|
163 | 168 | foreach ($attributes as $attributeValue) {
|
164 | 169 | if (isset($attributesMap[$attributeValue['attribute_id']])) {
|
165 | 170 | $entityData[$attributesMap[$attributeValue['attribute_id']]] = $attributeValue['value'];
|
166 | 171 | } else {
|
167 | 172 | $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']}' |
169 | 174 | for entity type '$entityType'."
|
170 | 175 | );
|
171 | 176 | }
|
172 | 177 | }
|
173 | 178 | }
|
174 | 179 | return $entityData;
|
175 | 180 | }
|
| 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 | + } |
176 | 209 | }
|
0 commit comments