PHP code example of contenir / contenir-db-queryfilter
1. Go to this page and download the library: Download contenir/contenir-db-queryfilter 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/ */
contenir / contenir-db-queryfilter example snippets
use Contenir\Db\QueryFilter\Filter\AbstractFilterSelect;
use Contenir\Db\QueryFilter\Filter\AbstractFilterText;
use PhpDb\Sql\Select;
final class SearchFilter extends AbstractFilterText
{
protected ?string $filterParam = 'search';
protected ?string $filterLabel = 'Search';
public function filter(Select $query): void
{
$value = $this->getFilterValue();
if (is_string($value) && $value !== '') {
$query->where->like('name', "%{$value}%");
}
}
}
final class CategoryFilter extends AbstractFilterSelect
{
protected ?string $filterParam = 'category';
protected ?string $filterLabel = 'Category';
public function getValueOptions(): array
{
return ['' => 'All categories', 'books' => 'Books', 'music' => 'Music'];
}
public function filter(Select $query): void
{
$value = $this->getFilterValue();
if ($value !== null) {
$query->where(['category' => $value]);
}
}
}
use Contenir\Db\QueryFilter\FilterSet;
use Contenir\Db\QueryFilter\Form;
$form = new Form('product-filter');
$form->setFilterSet(new FilterSet([new SearchFilter(), new CategoryFilter()]));
$form->build();
use Contenir\Db\QueryFilter\QueryFilter;
use Laminas\Paginator\Paginator;
$queryFilter = new QueryFilter($form);
$queryFilter->setQueryFilterTable($productRepository); // implements QueryFilterTableInterface
$queryFilter->setAdapter($adapter); // PhpDb\Adapter\AdapterInterface, for counting
$queryFilter->setQueryParams($request->getQueryParams());
$paginator = new Paginator($queryFilter->getPagingResultSet());
$paginator->setCurrentPageNumber((int) ($request->getQueryParams()['page'] ?? 1));
final class ProductRepository extends Repository implements QueryFilterTableInterface
{
public function __construct(EntityManager $em)
{
parent::__construct($em, Product::class);
}
}
use Contenir\Db\QueryFilter\RepositoryTable;
$queryFilter->setQueryFilterTable(new RepositoryTable($em->getRepository(Product::class)));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.