simtecsystem/cakephp-filter

Filter plugin for CakePHP >3.6

Installs: 12

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 2

Type:cakephp-plugin

1.1.1 2019-01-15 17:33 UTC

This package is auto-updated.

Last update: 2024-04-16 04:52:01 UTC


README

Software License Total Downloads Latest Stable Version Build Status Coverage Status

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require simtecsystem/cakephp-filter

Load the plugin

Add following to your config/bootstrap.php

Plugin::load('BRFilter');

Usage

Controller class

public function index()
{
		$this->loadComponent('BRFilter.Filter');
		
		// add filter and options
		$this->Filter->addFilter([
					'filter_id' => ['field' => 'Posts.id', 'operator'=>'='],
					'filter_title' => ['field' => 'Posts.title', 'operator' => 'LIKE', 'explode' => 'true'],
					'filter_category_id' => ['field'=> 'Posts.category_id', 'operator' => 'IN' ] 
		]);
		
		// get conditions
		$conditions = $this->Filter->getConditions(['session'=>'filter']);
		
		// set url for pagination
    	$this->set('url', $this->Filter->getUrl());
    	
    	// apply conditions to pagination
    	$this->paginate['conditions']	= $conditions;
    	
    	// get pagination 
    	$this->set('posts', $this->paginate($this->Posts));
    	
    	// ...
}

Template views

You have to add a form to your index.ctp, corresponding with the alias of your filter configuration.

	echo $this->Form->create();
    
   	// Match with the filter configuration in your controller 
    echo $this->Form->input('filter_id', ['type' => 'text']);
    echo $this->Form->input('filter_title', ['type' => 'text']);
    echo $this->Form->input('filter_category_id', ['options'=>[ /* ... */ ], 'multiple'=>'multiple' ]);
    
	echo $this->Form->button('Filter', ['type' => 'submit']);
	echo $this->Form->end();

Filter options

The following options are supported:

  • field (string) The name of the field to use for searching.
  • operator (string) The operator used for searching.
  • explode (boolean) Used only with operator LIKE and ILIKE to explode the string query.

Operators

The following options are supported:

  • =
  • >
  • <
  • >=
  • <=
  • LIKE
  • ILIKE
  • IN

Persisting query (session)

All query strings are persisted using sessions. Make sure to load the Session component.