yiisoft / data

Data providers

Home Page:https://www.yiiframework.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filters do not support their custom process

kamarton opened this issue · comments

What steps will reproduce the problem?

Currently cannot implement own filter. For example, I want a regexp filter, or custom group filter with scoring system.

What is the expected result?

        $filter = new CustomRegexpFilter('name' /* field */, '!^a[g]?!' /* pattern */, 'ui' /* modifier*/);
        $reader = (new ArrayDataReader($this->getDataSet()))
            ->withFilter($filter);

        $filter = new CustomScoringGroupFilter(new Filter(), new Filter()...);
        $reader = (new ArrayDataReader($this->getDataSet()))
            ->withFilter($filter);

What do you get instead?

This function throw exception if i using custom filter. As I look at eg. ArrayDataReader, I have no chance of implementing a custom filter (for example, inheritance, but all filtering functions are private).

private function matchFilter(array $item, array $filter): bool
Q A
Version 1.0.0 under development
PHP version -
Operating system -

That is correct. I found no easy way to implement that because how filter is applied differs per data reader implementation. In array reader that's directly checking an item, in SQL reader that's forming a query.

If you have idea on how to implement it, I'd love to hear it.

In this form, it severely limits the opportunity. Simply put, I know I use ArrayDataProvder or ActiveDataProvider, so why can't I add my own custom filter.

At worst, the filter you create is limited to some DataProviders.

Suggest: different data providers need to define separate interfaces, and if you define it on a filter, you can use it.

In this case, e.g. the regexp filter:

  • define function for ArrayDataProvider: preg_match(...)
  • define function for ActiveDataProvider: new Expression('REGEXP ...')
  • not define function for XxxxxDataProvider: throw exception not supported filter.

I want to build GridView on top of this package and it should not be aware which data provider is used and rely on interface.

GridView also has to work with Filter classes, I see no reason to define a GridView custom ColumnFilter, which can also be defined by Custom Filter.

I will think about this problem, but I think there is something wrong between the layers if you don't allow it.

Yes, I'd like to allow it as well. Just had no immediate idea on how to do it best.

Although I was thinking of a similar solution, I feel a bit hurried about the topic. It is true that it is currently applicable on its own at this layer, but as the upper layers enter, it may not be the right solution at all. It seems to be a detour rather than a layered approach.

This one is solved now. PR is to be merged soon.