PHPackages                             gildonei/filter\_results - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. gildonei/filter\_results

ActiveCakephp-plugin[Search &amp; Filtering](/categories/search)

gildonei/filter\_results
========================

CakePHP Plugin to add PRG filters to controllers and views

1.0(5y ago)3156CC-BY-3.0PHPPHP &gt;=5.6

Since Jul 28Pushed 3y agoCompare

[ Source](https://github.com/gildonei/filter_results)[ Packagist](https://packagist.org/packages/gildonei/filter_results)[ Docs](https://github.com/gildonei/filter_results)[ RSS](/packages/gildonei-filter-results/feed)WikiDiscussions 2.3 Synced 6d ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Filter Results
==============

[](#filter-results)

Generates `conditions` to `find` methods in CakePHP 2.3+ from a search form.

Compatibility
-------------

[](#compatibility)

Compatible with CakePHP 2.3 + Paginate (Component)

- Version for CakePHP 1.3: [http://github.com/pedroelsner/filter\_results/tree/1.3](http://github.com/pedroelsner/filter_results/tree/1.3 "FilterResults para CakePHP 1.3")
- Version for CakePHP 2.0: [http://github.com/pedroelsner/filter\_results/tree/2.0](http://github.com/pedroelsner/filter_results/tree/2.0 "FilterResults para CakePHP 2.0")

Changes in version 2.3
======================

[](#changes-in-version-23)

- `FilterResultsComponent` was changed to `FilterComponent`;
- `FilterFormHelper` was changed to `SearchHelper`;
- Refectory all struture methods;

Installation
============

[](#installation)

Composer
--------

[](#composer)

`composer require gildonei/filter_results:"2.3.x-dev"`

Manual download
---------------

[](#manual-download)

Download the plugin and place its contents inside `/app/Plugin/FilterResults` or other directory plugins for CakePHP.

Activation
----------

[](#activation)

Activate the plugin by adding the file **/app/Config/bootstrap.php**:

```
CakePlugin::load('FilterResults');
```

Configuration
-------------

[](#configuration)

Edit the file **/app/AppController.php**:

```
var $components = array(
    'FilterResults.Filter' => array(
        'auto' => array(
            'paginate' => false,
            'explode'  => true,  // recommended
        ),
        'explode' => array(
            'character'   => ' ',
            'concatenate' => 'AND',
        )
    )
);

var $helpers = array(
    'FilterResults.Search' => array(
        'operators' => array(
            'LIKE'       => 'containing',
            'NOT LIKE'   => 'not containing',
            'LIKE BEGIN' => 'starting with',
            'LIKE END'   => 'ending with',
            '='  => 'equal to',
            '!=' => 'different',
            '>'  => 'greater than',
            '>=' => 'greater or equal to',
            'Filter->addFilters(
        array(
            'filter1' => array(
                'User.name' => array(
                    'operator' => 'LIKE',
                    'value' => array(
                        'before' => '%', // optional
                        'after'  => '%'  // optional
                    )
                )
            )
        )
    );

    $this->Filter->setPaginate('order', 'User.name ASC'); // optional
    $this->Filter->setPaginate('limit', 10);              // optional

    // Define conditions
    $this->Filter->setPaginate('conditions', $this->Filter->getConditions());

    $this->User->recursive = 0;
    $this->set('users', $this->paginate());
}
```

The setting here is quite simple: We create a filter called `filter1` that will use the field `User.name`. This filter uses the `LIKE` operator add `%` before and after the content to be filtered.

Now we just have to make the form on View at the top of the table.

File **/app/View/Users/index.ctp)**

```
echo $this->Search->create();
echo $this->Search->input('filter1');
echo $this->Search->end(__('Filter', true));
```

Ready! We have a field that filters the user by name and compatible with the Paginate.

And more, the Filter Results automaticaly explode the filter value to gain a better results. For example: if we filter by 'Pedro Elsner', the condition will be: `WHERE ((User.name LIKE '%Pedro%') AND (User.name LIKE '%Elsner%'))`

Explode Settings
----------------

[](#explode-settings)

The option `explode` for operators `LIKE` and `NOT LIKE` is always enabled in the settings of the Filter Results. But, how do you know, you can disable it into components declaration in controller. If you do, you can enable the `explode` function for only the specified filter:

```
$this->Filter->addFilter(
    array(
        'filter1' => array(
            'User.name' => array(
                'operator' => 'LIKE',
                'explode'  => true
            )
        )
    )
);
```

Too is possible to change the explode parameters for each filter.

```
$this->Filter->addFilter(
    array(
        'filter1' => array(
            'User.name' => array(
                'operator' => 'LIKE',
                'explode' => array(
                    'character'   = '-',
                    'concatenate' = 'OR'
                )
            )
        )
    )
);
```

Also, you can to use the explode function with any operator (like `=`). See:

```
$this->Filter->addFilter(
    array(
        'filter1' => array(
            'User.name' => array(
                'operator' => '=',
                'explode'  => true
            )
        )
    )
);
```

Simple Filter + Composite Rule
==============================

[](#simple-filter--composite-rule)

Let us now make another rule within the filter `filter1`. We want to filter it by name (`User.name`) or by username(`User.username`).

Then just changed our Controller:

File **/app/Controller/UsersController.php**

```
$this->Filter->addFilters(
    array(
        'filter1' => array(
            'OR' => array(
                'User.name'     => array('operator' => 'LIKE'),
                'User.username' => array('operator' => 'LIKE')
            )
        )
    )
);
```

The rule `OR` can also be `AND` or `NOT`.

**NOTE**: If you define more than one condition without the specific rule, the plugin will understand automatically how `AND`.

Simple Filter + Fixed Rules
===========================

[](#simple-filter--fixed-rules)

Suppose now that our filter `filter1` when informed should filter by name (`User.name`) **AND** only active users.

File **/app/Controller/UsersController.php**

```
$this->Filter->addFilters(
    array(
        'filter1' => array(
            'User.name'   => array('operator' => 'LIKE'),
            'User.active' => array('value'    => '1')
        )
    )
);
```

Filters Aggregation
===================

[](#filters-aggregation)

The Filter Results automatically concatenates all filter by the rule `AND`. See in the follow example, that if informe 'Pedro' in `filter1` and 'elsner' in `filter2` we going to get the condition: `WHERE (User.name LIKE '%Pedro%') AND (User.usernname LIKE '%elsner%')`

```
$this->Filter->addFilters(
    array(
        'filter1' => array(
            'User.name' => array('operator' => 'LIKE')
        )
        'filter2' => array(
            'User.username' => array('operator' => 'LIKE')
        )
    )
);
```

**NOTE**: We too can concatenate the filters by rules `OR` and `NOT`.

Now, we going to change the example for concatenate the `filter1` and `filter2` by rule `OR`, and, if `filter1` is not empty, only the active users. So, we going to get this condition: `WHERE ((User.name LIKE '%Pedro%') AND (User.active = 1)) OR (User.usernname LIKE '%elsner%')`

```
$this->Filter->addFilters(
    array(
        'OR' => array(
            'filter1' => array(
                'User.name'   => array('operator' => 'LIKE'),
                'User.active' => array('value'    => '1')
            )
            'filter2' => array(
                'User.username' => array('operator' => 'LIKE')
            )
        )
    )
);
```

Selection Filter
================

[](#selection-filter)

Let's change our filter. In addition to filtering by name, we now also filter by user group(`Group.name`) through a desired selection field.

File **/app/Controller/UsersController.php**

```
function index() {

    // Add filter
    $this->Filter->addFilters(
        array(
            'filter1' => array(
                'User.name' => array('operator' => 'LIKE')
            ),
            'filter2' => array(
                'User.group_id' => array(
                    'select' => $this->Filter->select('Grupo', $this->User->Group->find('list'))
                )
            )
        )
    );

    // Define conditions
    $this->Filter->setPaginate('conditions', $this->Filter->getConditions());

    $this->User->recursive = 0;
    $this->set('users', $this->paginate());
}
```

File **/app/View/Users/index.ctp**

```
echo $this->Search->create();
echo $this->Search->input('filter2', array('class' => 'select-box'));
echo $this->Search->input('filter1');
echo $this->Search->end(__('Filter', true));
```

Ready! Use and abuse of those filters you want.

To be clear, see this [image](http://pedroelsner.com/wp-content/uploads/2011/09/filterResults_1.png). Here we have the Product View, where you can filter by: Color, Size, Weight, Mateiral and Product Name.

Advanced Filter
===============

[](#advanced-filter)

In some cases we want something different than, for example, we want the user to choose the field and the operator to perform the filter.

File **/app/Controller/UsersController.php**

```
function index() {

    // Add filter
    $this->Filter->addFilters('filter1');

    // Define conditions
    $this->Filter->setPaginate('conditions', $this->Filter->getConditions());

    $this->User->recursive = 0;
    $this->set('users', $this->paginate());
}
```

**NOTE**: Note that this time we filter `filter1` without any parameters. This is because the rules are selected in the View.

File **/app/View/Users/index.ctp**

```
echo $this->Search->create();
echo $this->Search->selectFields('filter1', null, array('class' => 'select-box'));
echo $this->Search->selectOperators('filter1');
echo $this->Search->input('filter1');
echo $this->Search->end(__('Filter', true));
```

Now, you can first select the field (automatically the Filter Results table lists the fields), then inform the operator and the desired value for the filter.

There will be situations where you need to customize the fields and operators for selection. For example, let's just leave the fields `User.id`, `User.name` and `User.username` for selection, and the operators `LIKE` and `=`.

For this, we change only the View.

Arquivo **/app/View/Users/index.ctp**

```
echo $this->Search->create();

echo $this->Search->selectFields('filter1',
        array(
            'User.id'       => __('ID', true),
            'User.name'     => __('Name', true),
            'User.username' => __('Username', true),
        ),
        array(
            'class' => 'select-box'
        )
    );

echo $this->Search->selectOperators('filter1',
        array(
            'LIKE' => __('containing', true),
            '='    => __('equal to', true)
        )
    );

echo $this->Search->input('filter1');
echo $this->Search->end(__('Filter', true));
```

Operators
=========

[](#operators)

The Filter results have pre-defined operators, below you will find all the options available for you to use in their advanced filters.

```
array(
    'LIKE'       => __('containing', true),
    'NOT LIKE'   => __('not containing', true),
    'LIKE BEGIN' => __('starting with', true),
    'LIKE END'   => __('ending with', true),
    '='  => __('equal to', true),
    '!=' => __('different', true),
    '>'  => __('greater than', true),
    '>=' => __('greater or equal to', true),
    '
