Skip to content

Commit bd9e1c8

Browse files
committed
MAGETWO-59555: Mass Actions are slow
- Squashed all the commits into one - Added code so that instead of the whole collection of objects being pulled in, we only pull an array of ids.
1 parent 5ffafe2 commit bd9e1c8

File tree

5 files changed

+430
-14
lines changed

5 files changed

+430
-14
lines changed

app/code/Magento/Ui/Component/MassAction/Filter.php

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\RequestInterface;
1212
use Magento\Framework\View\Element\UiComponentInterface;
1313
use Magento\Framework\Data\Collection\AbstractDb;
14+
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
1415

1516
/**
1617
* Class Filter
@@ -43,6 +44,11 @@ class Filter
4344
*/
4445
protected $filterBuilder;
4546

47+
/**
48+
* @var DataProviderInterface
49+
*/
50+
private $dataProvider;
51+
4652
/**
4753
* @param UiComponentFactory $factory
4854
* @param RequestInterface $request
@@ -74,23 +80,22 @@ public function getComponent()
7480
}
7581

7682
/**
83+
* Adds filters to collection using DataProvider filter results
84+
*
7785
* @param AbstractDb $collection
7886
* @return AbstractDb
7987
* @throws LocalizedException
8088
*/
8189
public function getCollection(AbstractDb $collection)
8290
{
83-
$component = $this->getComponent();
84-
$this->prepareComponent($component);
85-
$dataProvider = $component->getContext()->getDataProvider();
86-
$dataProvider->setLimit(0, false);
87-
$ids = [];
88-
foreach ($dataProvider->getSearchResult()->getItems() as $document) {
89-
$ids[] = $document->getId();
91+
$idsArray = $this->getFilterIds();
92+
if (!empty($idsArray)) {
93+
$collection->addFieldToFilter(
94+
$collection->getIdFieldName(),
95+
['in' => $idsArray]
96+
);
9097
}
91-
92-
$collection->addFieldToFilter($collection->getIdFieldName(), ['in' => $ids]);
93-
return $this->applySelection($collection);
98+
return $collection;
9499
}
95100

96101
/**
@@ -106,9 +111,7 @@ public function applySelectionOnTargetProvider()
106111
if ('false' === $excluded) {
107112
return;
108113
}
109-
$component = $this->getComponent();
110-
$this->prepareComponent($component);
111-
$dataProvider = $component->getContext()->getDataProvider();
114+
$dataProvider = $this->getDataProvider();
112115
try {
113116
if (is_array($excluded) && !empty($excluded)) {
114117
$this->filterBuilder->setConditionType('nin')
@@ -127,6 +130,8 @@ public function applySelectionOnTargetProvider()
127130
}
128131

129132
/**
133+
* Applies selection to collection from POST parameters
134+
*
130135
* @param AbstractDb $collection
131136
* @return AbstractDb
132137
* @throws LocalizedException
@@ -169,7 +174,7 @@ public function prepareComponent(UiComponentInterface $component)
169174
}
170175

171176
/**
172-
* Returns RefererUrl
177+
* Returns Referrer Url
173178
*
174179
* @return string|null
175180
*/
@@ -178,4 +183,33 @@ public function getComponentRefererUrl()
178183
$data = $this->getComponent()->getContext()->getDataProvider()->getConfigData();
179184
return (isset($data['referer_url'])) ? $data['referer_url'] : null;
180185
}
186+
187+
/**
188+
* Get data provider
189+
*
190+
* @return DataProviderInterface
191+
*/
192+
private function getDataProvider()
193+
{
194+
if (!$this->dataProvider) {
195+
$component = $this->getComponent();
196+
$this->prepareComponent($component);
197+
$this->dataProvider = $component->getContext()->getDataProvider();
198+
}
199+
return $this->dataProvider;
200+
}
201+
202+
/**
203+
* Get filter ids as array
204+
*
205+
* @return int[]
206+
*/
207+
private function getFilterIds()
208+
{
209+
$this->applySelectionOnTargetProvider();
210+
if ($this->getDataProvider()->getSearchResult()) {
211+
return $this->getDataProvider()->getSearchResult()->getAllIds();
212+
}
213+
return [];
214+
}
181215
}

0 commit comments

Comments
 (0)