Create filter group inside foreach dynamic filters

by mileven   Last Updated May 28, 2018 10:09 AM

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'
         ],
         0 => [
           'field' => 'status',
           'value' => 'upcoming',
           'condition_type' => 'eq'
         ]
      ]
    ]
  ]

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
      }
   }
}


Related Questions


Filter products by website using AND

Updated July 14, 2015 13:04 PM

Collection with custom data not filtering

Updated June 26, 2015 19:04 PM