PHP code example of intellect-web-development / symfony-doctrine-search-bundle

1. Go to this page and download the library: Download intellect-web-development/symfony-doctrine-search-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/ */

    

intellect-web-development / symfony-doctrine-search-bundle example snippets


// config/bundles.php
IWD\SymfonyDoctrineSearch\SymfonyDoctrineSearchBundle::class => ['all' => true],

use IWD\SymfonyDoctrineSearch\Query\Filter;
use IWD\SymfonyDoctrineSearch\Query\FilterOperator;
use IWD\SymfonyDoctrineSearch\Query\SearchQuery;
use IWD\SymfonyDoctrineSearch\Search\EntitySearch;

#[Route('/api/books.{_format}', methods: ['GET'])]
public function action(SearchQuery $searchQuery, EntitySearch $search, CurrentUser $user): Response
{
    // SearchQuery собирается из GET-параметров. Ограничение владельцем задаёт сервер:
    // клиентский фильтр по тому же свойству убираем, ограничение применяется всегда.
    $searchQuery = $searchQuery
        ->withoutFilters('owner.id')
        ->withConstraint(new Filter('owner.id', FilterOperator::Equals, $user->id));

    $result = $search->search(Book::class, $searchQuery);

    return $this->json([
        'data' => array_map(static fn (Book $book) => new BookOutput($book), $result->entities),
        'pagination' => $result->pagination, // {count, totalPages, page, size}
    ]);
}

use IWD\SymfonyDoctrineSearch\Search\EntityReader;

$book = $reader->get(Book::class, $id, [new Filter('owner.id', FilterOperator::Equals, $user->id)]);
// не найдена или чужая → EntityNotFoundException; find() вместо get() вернёт null

class QueryParams extends SearchQueryParameters
{
    #[OA\Property(property: 'filter', type: 'object', example: ['title' => ['like' => 'war']])]
    public array $filter = [];
}