public function getWhereFromApiCriteria(\Magento\Framework\Api\Search\SearchCriteriaInterface $criteria, \Praxigento\Core\Repo\Query\Criteria\IMapper $mapper = null) { $result = ''; $filterGroups = $criteria->getFilterGroups(); foreach ($filterGroups as $filterGroup) { $processed = []; // I don't know what is it "filter group" so uniquelize conditions inside one group only /** @var \Magento\Framework\Api\Filter $item */ foreach ($filterGroup->getFilters() as $item) { $field = $item->getField(); if ($mapper) { $field = $mapper->get($field); } $cond = $item->getConditionType(); $value = $item->getValue(); $where = $this->_conn->prepareSqlCondition($field, [$cond => $value]); if (!in_array($where, $processed)) { $result .= "({$where}) AND "; $processed[] = $where; } } } $result .= '1'; return $result; }
/** * @param SearchCriteriaInterface $searchCriteria * @return SearchResultInterface */ public function search(SearchCriteriaInterface $searchCriteria) { $collection = $this->collectionFactory->getReport($searchCriteria->getRequestName()); $collection->setPageSize($searchCriteria->getPageSize()); $collection->setCurPage($searchCriteria->getCurrentPage()); $this->filterPool->applyFilters($collection, $searchCriteria); foreach ($searchCriteria->getSortOrders() as $sortOrder) { if ($sortOrder->getField()) { $collection->setOrder($sortOrder->getField(), $sortOrder->getDirection()); } } return $collection; }
/** * @param Collection $collection * @param SearchCriteriaInterface $criteria * @return void */ public function applyFilters(Collection $collection, SearchCriteriaInterface $criteria) { foreach ($criteria->getFilterGroups() as $filterGroup) { foreach ($filterGroup->getFilters() as $filter) { /** @var $filterApplier FilterApplierInterface*/ if (isset($this->appliers[$filter->getConditionType()])) { $filterApplier = $this->appliers[$filter->getConditionType()]; } else { $filterApplier = $this->appliers['regular']; } $filterApplier->apply($collection, $filter); } } }
/** * {@inheritdoc} */ public function search(SearchCriteriaInterface $searchCriteria) { $this->requestBuilder->setRequestName($searchCriteria->getRequestName()); $scope = $this->scopeResolver->getScope(); $this->requestBuilder->bindDimension('scope', $scope); foreach ($searchCriteria->getFilterGroups() as $filterGroup) { foreach ($filterGroup->getFilters() as $filter) { $this->addFieldToFilter($filter->getField(), $filter->getValue()); } } $this->requestBuilder->setFrom($searchCriteria->getCurrentPage() * $searchCriteria->getPageSize()); $this->requestBuilder->setSize($searchCriteria->getPageSize()); $request = $this->requestBuilder->create(); $searchResponse = $this->searchEngine->search($request); return $this->searchResponseBuilder->build($searchResponse)->setSearchCriteria($searchCriteria); }