I want to filter a resultset after it has been fetched. The reason for this is that I have a "price" field which is generated in real time (based on current currency rate and other calculations). So what I have done is a standard ActiveDataProvider and then I have used array_filter
PHP function to filter this resultset.
This displays correctly in my ListView widget, however the filtering is only working on the initial set of results, i.e. in my case 30 items.
Here is my code:
$query = $this->find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination'=> ['pageSize' => 30],
]);
$query->andFilterWhere([
'<=', 'weight', $this->weight,
]);
// get the models from the dataprovider
$results = $dataProvider->getModels();
// create empty array to hold items
$items = [];
foreach($results as $item)
{
$data = [
'item' => $item,
'category' => $item->category,
'price' => $item->getPriceCalculated(),
];
$items[] = $data;
}
// filter results on price
$items = array_filter($items, function($a) {
return $a['price'] >= $this->price_min && $a['price'] <= $this->price_max;
});
// assign modified items array to dataprovider
$dataProvider->setModels($items);
return $dataProvider;
Anybody know how I can use ActiveDataProvider and still use pagination / sorting etc with the custom filtered resultset?