PHPackages                             cakedc/search-filter - 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. cakedc/search-filter

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

cakedc/search-filter
====================

SearchFilter plugin for CakePHP

2.0.9(9mo ago)33.4k↓40.6%MITPHPPHP &gt;=8.1

Since Sep 12Pushed 5mo ago11 watchersCompare

[ Source](https://github.com/CakeDC/search-filter)[ Packagist](https://packagist.org/packages/cakedc/search-filter)[ RSS](/packages/cakedc-search-filter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (4)Versions (22)Used By (0)

CakeDC SearchFilter Plugin for CakePHP
======================================

[](#cakedc-searchfilter-plugin-for-cakephp)

[![Build Status](https://camo.githubusercontent.com/a340d835ec19221fca69e2c70b87c1fdf7a7081d8e4d71b12c381899c76ba343/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f43616b6544432f7365617263682d66696c7465722f63692e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/CakeDC/search-filter/actions?query=workflow%3ACI+branch%3Amain)[![Coverage Status](https://camo.githubusercontent.com/03061081d7136c63d6abdd39be0e777b80e2a8c895e94da1ed1d2802a9890dbb/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f67682f43616b6544432f7365617263682d66696c7465722e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/gh/CakeDC/search-filter)[![Downloads](https://camo.githubusercontent.com/520412afec88b90c408224418c4ec626fabf4b36c2cd8e2c5701b77594521cd7/68747470733a2f2f706f7365722e707567782e6f72672f43616b6544432f7365617263682d66696c7465722f642f746f74616c2e706e67)](https://packagist.org/packages/CakeDC/search-filter)[![License](https://camo.githubusercontent.com/d99fe13d1dc822a438e490f2a30fca390453a83f9ebcecd62aa8e3287da0e169/68747470733a2f2f706f7365722e707567782e6f72672f43616b6544432f7365617263682d66696c7465722f6c6963656e73652e737667)](https://packagist.org/packages/CakeDC/search-filter)

Versions and branches
---------------------

[](#versions-and-branches)

CakePHPCakeDC Users PluginTagNotes^5.0[2.0](https://github.com/cakedc/users/tree/2.next-cake5)2.0.0stable^4.5[1.0](https://github.com/cakedc/search-filter/tree/1.next-cake4)1.0.0stableOverview
--------

[](#overview)

The SearchFilter plugin is a powerful and flexible solution for implementing advanced search functionality in CakePHP applications. It provides a robust set of tools for creating dynamic, user-friendly search interfaces with minimal effort.

Features
--------

[](#features)

- Dynamic filter generation based on database schema
- Support for various filter types: string, numeric, date, datetime, boolean, and lookup (autocomplete)
- Customizable filter conditions (equals, not equals, greater than, less than, between, etc.)
- Vue.js based frontend for an interactive user experience
- AJAX-powered autocomplete functionality for lookup filters
- Easy integration with CakePHP's ORM for efficient query building
- Extensible architecture allowing for custom filter types and conditions

Installation
------------

[](#installation)

You can install this plugin into your CakePHP application using [composer](https://getcomposer.org):

```
composer require cakedc/search-filter

```

Then, add the following line to your application's `src/Application.php` file:

```
$this->addPlugin('CakeDC.SearchFilter');
```

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

[](#configuration)

- [Criteria List](docs/Criteria.md)
- [Filters List](docs/Filters.md)

Basic Usage
-----------

[](#basic-usage)

### Controller

[](#controller)

In your controller, you can set up the search functionality like this:

```
use CakeDC\SearchFilter\Manager;

class PostsController extends AppController
{
    public function index()
    {
        $query = $this->Posts->find();

        $manager = new Manager($this->request);
        $collection = $manager->newCollection();

        // Add a general search filter
        $collection->add('search', $manager->filters()
            ->new('string')
            ->setConditions(new \stdClass())
            ->setLabel('Search...')
        );

        // Add a complex name filter that searches across multiple fields
        $collection->add('name', $manager->filters()
            ->new('string')
            ->setLabel('Name')
            ->setCriterion(
                $manager->criterion()->or([
                    $manager->buildCriterion('title', 'string', $this->Posts),
                    $manager->buildCriterion('body', 'string', $this->Posts),
                    $manager->buildCriterion('author', 'string', $this->Posts),
                ])
            )
        );

        // Add a datetime filter for the 'created' field
        $collection->add('created', $manager->filters()
            ->new('datetime')
            ->setLabel('Created')
            ->setCriterion($manager->buildCriterion('created', 'datetime', $this->Posts))
        );

        // Automatically add filters based on the table schema
        $manager->appendFromSchema($collection, $this->Posts);

        // Get the view configuration for the filters
        $viewFields = $collection->getViewConfig();
        $this->set('viewFields', $viewFields);

        // Apply filters if search parameters are present in the request
        if (!empty($this->getRequest()->getQuery()) && !empty($this->getRequest()->getQuery('f'))) {
            $search = $manager->formatSearchData();
            $this->set('values', $search);

            // Add a custom 'multiple' filter using the CriteriaFilter
            $this->Posts->addFilter('multiple', [
                'className' => 'CakeDC/SearchFilter.Criteria',
                'criteria' => $collection->getCriteria(),
            ]);

            $filters = $manager->formatFinders($search);
            $query = $query->find('filters', params: $filters);
        }

        // Paginate the results
        $posts = $this->paginate($this->Filter->prg($query));
        $this->set(compact('posts'));
    }
}
```

This example demonstrates several key features of the SearchFilter plugin:

1. Creating a new `Manager` instance and filter collection.
2. Adding a general search filter that can be used for quick searches.
3. Creating a complex filter that searches across multiple fields using `OrCriterion`.
4. Adding a datetime filter for a specific field.
5. Automatically generating filters based on the table schema.
6. Applying filters when search parameters are present in the request.
7. Using the `CriteriaFilter` for handling multiple filter criteria.

### View

[](#view)

In your view, you can render the search component inside search form like this:

```
