Skip to content

Repository Filter Groups Applied Inconsistently #4287

@astorm

Description

@astorm

Per conventional wisdom that's floating around the Magento programmer community, it's my understanding that, when creating object repositories and implementing a getList method that

  1. Filters within a group should be applied as OR filters
  2. Filter groups themselves should be applied as AND filter

However, this informal guideline is not followed consistently in the core Magento systems code. For example, the CMS page repository does not apply this AND/OR behavior, and applies each filter as an AND

#File: vendor/magento/module-cms/Model/PageRepository.php
foreach ($criteria->getFilterGroups() as $filterGroup) {
    foreach ($filterGroup->getFilters() as $filter) {
        if ($filter->getField() === 'store_id') {
            $collection->addStoreFilter($filter->getValue(), false);
            continue;
        }
        $condition = $filter->getConditionType() ?: 'eq';
        $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
    }
}

Whereas the coupon repository does follow this guideline.

#File: vendor/magento/module-sales-rule/Model/CouponRepository.php

protected function addFilterGroupToCollection(
    \Magento\Framework\Api\Search\FilterGroup $filterGroup,
    Collection $collection
) {
    $fields = [];
    $conditions = [];
    foreach ($filterGroup->getFilters() as $filter) {
        $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
        $fields[] = $filter->getField();
        $conditions[] = [$condition => $filter->getValue()];
    }
    if ($fields) {
        $collection->addFieldToFilter($fields, $conditions);
    }
}

This makes it unclear how filters and filter groups should be applied.

Expected Behavior: Ideally, all repositories with a getList method that accepts search criteria should behave the same, and apply a "filters are OR", "filter groups are AND" logic. If you really wanted to knock this out of the park, a helper or trait that provided and ventral consistent place for filter group adding logic would be ideal. This would remove the burden from the repository creator as to how filters and filter groups should be applied.

If neither is possible, then the behavior of individual repositories should b documented.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions