Filter Interfaces
The filter interfaces provide access to filters and filter rules defined in the backend within a MetaModel.
In addition, further filters can be created programmatically or filter parameters can be set.
IFilterRule Interface
Current information at: IFilterRule
Interfaces:
getMatchingIds()
returns all IDs matching the given filter rule
IFilter Interface
Current information at: IFilter
Interfaces:
addFilterRule(IFilterRule $objFilterRule)
adds a filter rule to the filter chain
getMatchingIds()
returns all IDs matching the given filter rule
createCopy()
creates a copy of the filter
Examples
The “filtering” blocks between “Start” and “End” are alternatives to each other. The called classes should be imported with a qualified “use” statement.
1<?php
2// Start
3$modelName = 'mm_employees';
4$factory = \Contao\System::getContainer()->get('metamodels.factory');
5// alternatively
6//$factory = $this->getContainer()->get('metamodels.factory');
7$model = $factory->getMetaModel($modelName);
8$filter = $model->getEmptyFilter();
9
10// Filter by fixed ID (list):
11$idList = [1,2,3];
12$filter->addFilterRule(new \MetaModels\Filter\Rules\StaticIdList($idList));
13
14// Filter by attribute value:
15$value = 'marketing';
16$languages = $model->getAvailableLanguages();
17$attribute = $model->getAttribute('division');
18$filter->addFilterRule(new \MetaModels\Filter\Rules\SearchAttribute($attribute, $value, $languages));
19
20// Custom SQL:
21$query = \sprintf('SELECT * FROM %s WHERE published = 1', $modelName);
22$filter->addFilterRule(new \MetaModels\Filter\Rules\SimpleQuery($query));
23// Alternative see https://www.doctrine-project.org/projects/doctrine-dbal/en/4.2/reference/data-retrieval-and-manipulation.html
24$query = \sprintf('SELECT * FROM %s WHERE published = ?', $modelName);
25$filter->addFilterRule(new \MetaModels\Filter\Rules\SimpleQuery($query, [1]));
26
27// Filter with multiple rules:
28// Combine with ConditionAnd() or ConditionOr()
29// Comparison with GreaterThan, LessThan, NotEqual possible
30$attribute = $model->getAttribute('price');
31$compareInclusive = true;
32$andRule = new \MetaModels\Filter\Rules\Condition\ConditionAnd();
33$andRule
34 ->addRule(new \MetaModels\Filter\Rules\Comparing\GreaterThan($attribute, 10, $compareInclusive)) // >= 10
35 ->addRule(new \MetaModels\Filter\Rules\Comparing\LessThan($attribute, 20)); // < 20
36$filter->addFilterRule($andRule);
37
38// End
39$items = $model->findByFilter($filter);
40$arrItems = $items->parseAll('text');
41//dump($arrItems);