2

I'm following this link to create a filter group to be applied to my custom collection.

searchCriteria => [
  'filterGroups' => [
    0 => [
      'filters' => [
         0 => [
           'field' => 'date',
           'value' => '2018-05-31',
           'condition_type' => 'gt'
         ],
         1 => [
           'field' => 'venue',
           'value' => 'staples_center',
           'condition_type' => 'eq'
         ]
    1 => [
      'filters' => [
         0 => [
           'field' => 'audience',
           'value' => '15000',
           'condition_type' => 'gt'
         ],
         1 => [
           'field' => 'status',
           'value' => 'upcoming',
           'condition_type' => 'eq'
         ],
         2 => [
           'field' => 'performer',
           'value' => '%simple%',
           'condition_type' => 'like'
         ],
      ]
    ]
  ]

The above input in one of the example, the problem is the input will be in dynamic array so i need to create the foreach filter groups child. However i dont know hot to create the filterGroupBuilder inside this for each.

public function __construct(
\Namespace\Module\Model\ConcertFactory $concert,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Framework\Api\FilterBuilder $filterBuilder,
\Magento\Framework\Api\Search\FilterGroupBuilder $filterGroupBuilder
){
 $this->concert = $concert;
 $this->searchCriteriaBuilder = $searchCriteriaBuilder;
 $this->filterBuilder = $filterBuilder;
 $this->filterGroupBuilder = $filterGroupBuilder;
}

public function execute(){
  $searchCriteria = $this->request->getPostValue('searchCriteria');
  if (array_key_exists("filterGroups",$searchCriteria)) {
      foreach ($searchCriteria['filterGroups'] as $filterGroup) {
         foreach ($filterGroup['filters'] as $filter) {
               $filterBuilder[] = $this->filterBuilder->setField($filter['field'])
              ->setValue($filter['value'])
              ->setConditionType($filter['condition_type'])
              ->create();
         }
         //need to know how to group this filter builder here
      }
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.