Magento2: OR Condition in searchCriteria (UI Component DataProvider) doesn't work

Created on 1 Oct 2018  路  10Comments  路  Source: magento/magento2

Preconditions

  1. Magento Commerce 2.2.5
  2. PHP version 7.1.20

Steps to reproduce

  1. I want to extend Order Status filter and allow to search both "Pending" and "Pending Payment" statuses when I select "Pending" in the filter. I extend filters for Status column of Admin Order's grid UI component so that the searchCriteria has two filters inside one filterGroup
"search_criteria": {
    "filter_groups": [
        {
            "filters": [
                {
                    "field": "status",
                    "value": "pending",
                    "condition_type": "eq"
                },
                {
                    "field": "status",
                    "value": "pending_payment",
                    "condition_type": "eq"
                }
            ]
        }
    ]
}

It can be extended vi URL params or using plugin for \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider::getSearchCriteria() method

  1. Apply Filter by Status field

Expected result

  1. Orders with both Pending and Pending Payment statuses are shown

Actual result

  1. No orders is selected

The reason is in the wrong processing of searchCriteria in \Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool::applyFilters() method. The searchCriteria above should be transformed to the following SQL statement:
WHERE (status="pending" OR status="pending_payment")
but it is transformed to the following:
WHERE status="pending" AND status="pending_payment"

The correct processing of searchCriteria is provided here \Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor::process()

Ui Confirmed P2 ready for dev Reproduced on 2.2.x Reproduced on 2.3.x S2 Dev.Experience

Most helpful comment

I can confirm, still broken in 2.4.0. The above patch is still a good workaround, albeit needing slight alteration to apply to 2.4 codebase.

Related to https://github.com/magento/magento2/issues/24576

All 10 comments

Hi @martin-cod. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me $VERSION instance

where $VERSION is version tags (starting from 2.2.0+) or develop branches (for example: 2.3-develop).
For more details, please, review the Magento Contributor Assistant documentation.

@martin-cod do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [x] yes
  • [ ] no

I can confirm that behavior. Reproduced also on 2.3-develop
That's happen because

class Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider implements DataProviderInterface 
{
    // only one way to add any filters
    public function addFilter(\Magento\Framework\Api\Filter $filter)
    {
        $this->searchCriteriaBuilder->addFilter($filter);
    }

    // unable to use searchcriteria because next filters will not be applied
    public function getSearchCriteria()
    {
        if (!$this->searchCriteria) {
            $this->searchCriteria = $this->searchCriteriaBuilder->create();
            $this->searchCriteria->setRequestName($this->name);
        }
        return $this->searchCriteria;
    }
}

class Magento\Framework\Api\Search\SearchCriteriaBuilder extends AbstractSimpleObjectBuilder
{
    // when creating criteria all filters goes in different groups, so only AND conditions
    public function create()
    {
        foreach ($this->filters as $filter) {
            $this->data[SearchCriteria::FILTER_GROUPS][] = $this->filterGroupBuilder->setFilters([])
                ->addFilter($filter)
                ->create();
        }
        $this->data[SearchCriteria::SORT_ORDERS] = [$this->sortOrderBuilder->create()];
        return parent::create();
    }
}

Any suggestions how to make a nice fix here?

Hi @engcom-backlog-nazar. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [x] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [x] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [x] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [x] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [x] 5. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [x] 6. Add label Issue: Confirmed once verification is complete.

  • [x] 7. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-backlog-nazar Thank you for verifying the issue. Based on the provided information internal tickets MAGETWO-96171, MAGETWO-96172 were created

Hi @martin-cod @yaroslav-zenin @engcom-backlog-nazar,
How you are able to reproduce the issue?
The Magento UI grid uses "condition_type": "in" incase of multi-select, and it will solve the purpose.
I didn't find any Filter in UI grid which create 2 eq condition_type on same field.

"search_criteria": {
    "filter_groups": [
        {
            "filters": [
                {
                    "field": "status",
                    "value": [ "pending_payment", "pending" ],
                    "condition_type": "in"
                }
            ]
        }
    ]
}

Hi @irajneeshgupta

I didn't try "condition_type": "in" but the SearchCriteria filter logic (described in the ticket details) is the different then it is described in Dev docs
https://devdocs.magento.com/guides/v2.2/extension-dev-guide/searching-with-repositories.html
https://devdocs.magento.com/guides/v2.2/rest/performing-searches.html

I assume, that the SearchCriteria filtering logic how it is applied in \Magento\Framework\View\Element\UiComponent\DataProviderFilterPool should be fixed to be consistent with DevDocs OR the exceptional filtering logic in UI DataProvider should be documented as well

Hello!

I changed the following sections to work OR filter:

In magento/framework/View/Element/UiComponent/DataProvider/FilterPool.php line 37

    /**
     * @param Collection $collection
     * @param SearchCriteriaInterface $criteria
     * @return void
     */
    public function applyFilters(Collection $collection, SearchCriteriaInterface $criteria)
    {
        foreach ($criteria->getFilterGroups() as $filterGroup) {
            $filters = $filterGroup->getFilters();
            $groupedFilters = [];
            $isGrouped = count($filters) > 1 ? true : false;

            foreach ($filterGroup->getFilters() as $filter) {
                $filterApplier = false;

                /** @var $filterApplier FilterApplierInterface*/
                if (isset($this->appliers[$filter->getConditionType()])) {
                    $filterApplier = $this->appliers[$filter->getConditionType()];
                } else {
                    if (!$isGrouped) {
                        $filterApplier = $this->appliers['regular'];
                    } else {
                        $groupedFilters[] = $filter;
                    }
                }
                if ($filterApplier) {
                    $filterApplier->apply($collection, $filter);
                }
            }

            if (count($groupedFilters) > 0) {
                $filterApplier = $this->appliers['regular'];
                $filterApplier->applyFilterGroup($collection, $groupedFilters);
                $groupedFilters = [];
            }
        }
    }

In magento/framework/View/Element/UiComponent/DataProvider/RegularFilter.php i create a function applyFilterGroup

    /**
     * Apply regular filter groups like collection filters
     *
     * @param Collection $collection
     * @param Array $filters
     * @return void
     */
    public function applyFilterGroup(Collection $collection, Array $filters)
    {
        $filterGroupAttrs = [];
        $filterGroupCond  = [];
        foreach ($filters as $filter) {
            $filterGroupAttrs[] = $filter->getField();
            $filterGroupCond[] = [$filter->getConditionType() => $filter->getValue()];
        }

        $collection->addFieldToFilter($filterGroupAttrs, $filterGroupCond);
    }

After this change the filters are working fine.

Regards,
Hugo Aguiar.

@hugodeaguiar Thanks very much. I created a patch-file for this issue, which is a bug in my eyes.

framework_ui_component_data_provider_fix.patch.zip

To apply this patch via composer:

  • create a new directory in your Magento-root, for instance: patches/composer/ and drop the file framework_ui_component_data_provider_fix.patch in there.
  • Write the following in your composer.json:
{
   ...
   "require": {
        "magento/product-community-edition": "2.3.3-p1",
        ... 
   },
   ...
   "extra": {
        ...
        "patches": {
            "magento/framework": {
                "Bugfix for OR-filtering in adminhtml": "patches/composer/framework_ui_component_data_provider_fix.patch"
            },
            ...
        }
    }
}
  • run composer update

Hi @engcom-Charlie. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


I can confirm, still broken in 2.4.0. The above patch is still a good workaround, albeit needing slight alteration to apply to 2.4 codebase.

Related to https://github.com/magento/magento2/issues/24576

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MauroNigrele picture MauroNigrele  路  3Comments

BenSpace48 picture BenSpace48  路  3Comments

kirashet666 picture kirashet666  路  3Comments

kandrejevs picture kandrejevs  路  3Comments

jzalenski picture jzalenski  路  3Comments