2

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'))

2 Answers 2

0

try this

$filter_group = [
        $this->filterGroupBuilder
            ->addFilter($filter[0])
            ->create(),
        $this->filterGroupBuilder
             ->addFilter($filter[1])
            ->addFilter($filter[2])
            ->create(),
    ];
2
  • It shows: SELECT main_table.* FROM Training5_VendorRepository AS main_table WHERE ((active_until >= '2016-01-29 13:31:12')) AND ((active_until <= '2016-03-03 00:00:00') OR (active_until < '2016-01-05 00:00:00')) but still don't show (active_until < '2016-01-05 00:00:00') rows. Commented Jan 29, 2016 at 13:42
  • I believe your query is [p v (q ^ r)]. Its equal to [(p v q) ^ (p v r)] and this one you can create with searchCriteriaBuilder.
    – ciemin
    Commented Jan 29, 2016 at 15:46
0

This doesnt work anymore on 2.1.3 https://github.com/magento/magento2/issues/8300

Please reference that ticket

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.