PHP code example of bugloos / query-filter-bundle

1. Go to this page and download the library: Download bugloos/query-filter-bundle 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/ */

    

bugloos / query-filter-bundle example snippets


/*
 * Filter books by title column
*/
//Get api/book/index?filter[title]=text_for_search
OR
$filters = [
    'title' => 'text_for_search',
];
/*
 * Filter books by title column
*/
//Get api/book/index?filter[title]=text_for_search
OR
$filters = [
    'title' => 'text_for_search',
];

use Bugloos\QueryFilterBundle\Service\QueryFilter;

public function index(
    Request $request,
    BookRepository $bookRepository,
    QueryFilter $queryFilter
): Response {
    $queryBuilder = $bookRepository->createQueryBuilder('b');
    
    $queryBuilder = $queryFilter->for($queryBuilder)
        ->parameters($request->get('filters'))
        ->filter()
    ;
    
    return $queryBuilder->getQuery()->getResult();
}

$mappers = [
    'country' => 'country.name',
];

/*
 * Filter books by country column
*/
//Get api/book/index?filter[country]=text_for_search
OR
$filters = [
    'country' => 'text_for_search',
];
/*
 * Filter books by country column
*/
//Get api/book/index?filter[country]=text_for_search
OR
$filters = [
    'country' => 'text_for_search',
];

public function index(
    Request $request,
    BookRepository $bookRepository,
    QueryFilter $queryFilter
): Response {
    $queryBuilder = $bookRepository->createQueryBuilder('b');
    
    $queryBuilder = $queryFilter->for($queryBuilder)
        ->parameters($request->get('filters'))
        ->addMapper('country', 'country.name')
        ->filter()
    ;
    
    return $queryBuilder->getQuery()->getResult();
}

$queryBuilder = $bookRepository->createQueryBuilder('b');
OR
$queryBuilder = $bookRepository->createQueryBuilder('b')
    ->addSelect('country')   
    ->leftJoin('b.country', 'country')      
;

$mapper = [
    'age' => 'bookUsers.user.age',
];

/*
 * Filter books by Writers age column
*/
//Get api/book/index?filter[age]=31
OR
$filters = [
    'age' => 31,
];
/*
 * Filter books by Writers age column
*/
//Get api/book/index?filter[age]=31
OR
$filters = [
    'age' => 31,
];

public function index(
    Request $request,
    BookRepository $bookRepository,
    QueryFilter $queryFilter
): Response {
    $queryBuilder = $bookRepository->createQueryBuilder('b');
    
    $queryBuilder = $queryFilter->for($queryBuilder)
        ->parameters($request->get('filters'))
        ->addMapper('age', 'bookUsers.user.age')
        ->sort()
    ;
    
    return $queryBuilder->getQuery()->getResult();
}

/*
 * Filter books by title and age
*/
//Get api/book/index?filter[title]=text&filter[age]=31
OR
$filters = [
    'title' => 'text',
    'age' => 31,
];

$queryBuilder = $queryFilter->for($queryBuilder)
    ->parameters($request->get('filters'))
    ->cacheTime(120)
    ->sort()
;