I have problem with magento2 FilterGroupBuilder. I need to set filter where active_until date is between today and March 3 OR active_until before January 5. My code looks like:
public function dot4()
{
$filter[] = $this->filterBuilder
->setConditionType('gteq')
->setField('active_until')
->setValue(date("Y-m-d H:i:s"))
->create();
$filter[] = $this->filterBuilder
->setConditionType('lteq')
->setField('active_until')
->setValue(date("Y-m-d H:i:s", strtotime('March 3')))
->create();
$filter[] = $this->filterBuilder
->setConditionType('lt')
->setField('active_until')
->setValue(date("Y-m-d H:i:s", strtotime('January 5')))
->create();
$filter_group = [
$this->filterGroupBuilder
->addFilter($filter[0])
->create(),
$this->filterGroupBuilder
->addFilter($filter[1])
->create(),
$this->filterGroupBuilder
->addFilter($filter[2])
->create(),
];
$this->searchCriteriaBuilder->setFilterGroups($filter_group);
$list = $this->vendorInterface->getList(
$this->searchCriteriaBuilder->create()
)->getItems();
return $list;
}
And MySQL query shows:
'SELECT `main_table`.* FROM `Training5_VendorRepository` AS `main_table` WHERE ((`active_until` >= '2016-01-29 12:06:25')) AND ((`active_until` <= '2016-03-03 00:00:00')) AND ((`active_until` < '2016-01-05 00:00:00'))' (length=216)
If i change $filter_group to that structure:
$filter_group = [
$this->filterGroupBuilder
->addFilter($filter[0])
->addFilter($filter[1])
->create(),
$this->filterGroupBuilder
->addFilter($filter[2])
->create(),
];
Query:
SELECT `main_table`.* FROM `Training5_VendorRepository` AS `main_table` WHERE ((`active_until` >= '2016-01-29 12:13:38') OR (`active_until` <= '2016-03-03 00:00:00')) AND ((`active_until` < '2016-01-05 00:00:00'))
But still it's not a solution... So how to group filters to show query like this:
SELECT `main_table`.* FROM `Training5_VendorRepository` AS `main_table` WHERE ((`active_until` >= '2016-01-29 12:13:38') AND (`active_until` <= '2016-03-03 00:00:00')) OR ((`active_until` < '2016-01-05 00:00:00'))