1. Go to this page and download the library: Download cakedc/search-filter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cakedc / search-filter example snippets
$this->addPlugin('CakeDC.SearchFilter');
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'));
}
}