dev-master
9999999-dev http://github.com/dogmatic69/cakephp-sifterCakePHP plugin for filtering and searching model data
MIT
The Requires
by Carl Sutton (dogmatic69)
cakephp
CakePHP plugin for filtering and searching model data
Sifter is a CakePHP plugin that makes searching and filtering data simple. It can also be configured to change how it searches., (*2)
All configuration is done in the model, which means there is no need to mess about with forms, redirects or any other code. A component interacts with request data and does various things depening on the data found in the request., (*3)
Main Features:, (*4)
The plugin is made up of the following main sections:, (*5)
The behavior does a number of things: - Figure out the form inputs based on the field schema - Find and call custom find methods in the model for fetching data - Configure what is searched and where, (*6)
This is the frontend to the Sifter plugin. This is totaly optional but should cover most usage cases. If you are not going to use the included element for the search form you should at least look at if for ideas as to what is required to make the search work., (*7)
Install with git, (*8)
git clone https://github.com/dogmatic69/cakephp-sifter Plugin/Sifter
Install as a submodule, (*9)
git submodule add https://github.com/dogmatic69/cakephp-sifter Plugin/Sifter
Install with composer (comming soon), (*10)
composer.phar require dogmatic69/cakephp-sifter
Attach the behavior to the model you would like to sift. This can be done in the actsAs property or dynamically, see the CakePHP docs for more details., (*11)
class MyModel extends AppModel { public $actsAs = array( ... other behaviors 'Sifter.Sifter' => array( 'fields' => array( 'MyModel.field_1' => array( 'input' => array( 'placeholder' => 'Customize your form with ease', ) ), 'MyOtherModel.field_2', 'field_3', // <- will automatically use the current model alias ) ), ); // code }
Once its attached to the model, you need to attach the component to the controller so that it can catch and deal with sifter requests. Again see the CakePHP docs for specifics, (*12)
class MyController extends AppController {, (*13)
public $components = array( ..., 'Sifter.Sifter', ); // code
}, (*14)
Finally you need to have a form so that users will be able to input the serch requirements. There is an included element which will build a generic search form based on the inputs that have been configured. Alternativly you could use the form helper to build your own., (*15)
echo $this->element('Sifter.sift');
That is it, now when you visit the page you should see a search form. Submitting data on in the form will do a PRG to to a URL containing all your params. If you are using pagination these will be included already in the PaginatorComponent::$settings
property. If you are using a normal find on the page you can use the SifterComponent::sift()
method to fetch the query conditions., (*16)
In a controller method:, (*17)
$conditions = $this->Sifter->sift($this); pr($conditions); // output array( 'conditions' => array( 'MyModel.field_1' => 'foobar', 'MyOtherModel.field_2' => 'baz', ... ), 'contain' => array( 'MyOtherModel', ) )
Submit bugs / patches on github.com/dogmatic69/cakephp-sifter, (*18)
CakePHP plugin for filtering and searching model data
MIT
cakephp