|
7 | 7 | namespace Magento\SalesRule\Model\ResourceModel\Rule; |
8 | 8 |
|
9 | 9 | use Magento\Framework\DB\Select; |
10 | | -use Magento\Framework\EntityManager\MetadataPool; |
11 | | -use Magento\Framework\Model\AbstractExtensibleModel; |
12 | 10 | use Magento\Framework\Serialize\Serializer\Json; |
13 | 11 | use Magento\Quote\Model\Quote\Address; |
14 | 12 | use Magento\SalesRule\Api\Data\CouponInterface; |
15 | | -use Magento\SalesRule\Api\Data\RuleInterface; |
16 | 13 | use Magento\SalesRule\Model\Coupon; |
17 | 14 | use Magento\SalesRule\Model\Rule; |
18 | 15 |
|
@@ -80,16 +77,12 @@ public function __construct( |
80 | 77 | \Magento\Framework\Stdlib\DateTime\TimezoneInterface $date, |
81 | 78 | \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, |
82 | 79 | \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null, |
83 | | - Json $serializer = null, |
84 | | - MetadataPool $metadataPool = null |
| 80 | + Json $serializer = null |
85 | 81 | ) { |
86 | 82 | parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource); |
87 | 83 | $this->_date = $date; |
88 | 84 | $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class); |
89 | | - $metadataPool = $metadataPool ?: |
90 | | - \Magento\Framework\App\ObjectManager::getInstance()->get(MetadataPool::class); |
91 | 85 | $this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap(); |
92 | | - $this->_setIdFieldName($metadataPool->getMetadata(RuleInterface::class)->getLinkField()); |
93 | 86 | } |
94 | 87 |
|
95 | 88 | /** |
@@ -120,35 +113,29 @@ protected function mapAssociatedEntities($entityType, $objectField) |
120 | 113 |
|
121 | 114 | $entityInfo = $this->_getAssociatedEntityInfo($entityType); |
122 | 115 | $ruleIdField = $entityInfo['rule_id_field']; |
123 | | - $entityIds = $this->getColumnValues($ruleIdField); |
| 116 | + |
| 117 | + $items = []; |
| 118 | + foreach ($this->getItems() as $item) { |
| 119 | + $items[$item->getData($ruleIdField)] = $item; |
| 120 | + } |
124 | 121 |
|
125 | 122 | $select = $this->getConnection()->select()->from( |
126 | 123 | $this->getTable($entityInfo['associations_table']) |
127 | 124 | )->where( |
128 | 125 | $ruleIdField . ' IN (?)', |
129 | | - $entityIds |
| 126 | + array_keys($items) |
130 | 127 | ); |
131 | 128 |
|
132 | 129 | $associatedEntities = $this->getConnection()->fetchAll($select); |
133 | | - $ruleIdFieldName = $this->getIdFieldName(); |
134 | | - if (empty($this->getIdFieldName()) && $this->getNewEmptyItem() instanceof AbstractExtensibleModel) |
135 | | - { |
136 | | - /** @var AbstractExtensibleModel $item */ |
137 | | - $ruleIdFieldName = $this->getNewEmptyItem()->getIdFieldName(); |
| 130 | + |
| 131 | + $dataToAdd = []; |
| 132 | + foreach ($associatedEntities as $associatedEntity) { |
| 133 | + //group data |
| 134 | + $dataToAdd[$associatedEntity[$ruleIdField]][] = $associatedEntity[$entityInfo['entity_id_field']]; |
| 135 | + } |
| 136 | + foreach ($dataToAdd as $id => $value) { |
| 137 | + $items[$id]->setData($objectField, $value); |
138 | 138 | } |
139 | | - array_map( |
140 | | - function ($associatedEntity) use ($entityInfo, $ruleIdField, $objectField, $ruleIdFieldName) { |
141 | | - if ($ruleIdField === $ruleIdFieldName) { |
142 | | - $item = $this->getItemById($associatedEntity[$ruleIdField]); |
143 | | - } else { |
144 | | - $item = $this->getItemByColumnValue($ruleIdField, $associatedEntity[$ruleIdField]); |
145 | | - } |
146 | | - $itemAssociatedValue = $item->getData($objectField) ?? []; |
147 | | - $itemAssociatedValue[] = $associatedEntity[$entityInfo['entity_id_field']]; |
148 | | - $item->setData($objectField, $itemAssociatedValue); |
149 | | - }, |
150 | | - $associatedEntities |
151 | | - ); |
152 | 139 | } |
153 | 140 |
|
154 | 141 | /** |
|
0 commit comments