brenoroosevelt/cakephp-filter

Filter plugin for CakePHP 3.x

Installs: 972

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 0

Forks: 2

Open Issues: 2

Type:cakephp-plugin

1.0.0 2017-01-25 19:00 UTC

This package is auto-updated.

Last update: 2024-10-10 21:12:43 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 brenoroosevelt/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.