1. Go to this page and download the library: Download cta-k12/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/ */
use CTA\SearchBundle\Annotation as Search; // Import the search bundle annotations
// ...
/**
* @ORM/Entity(repositoryClass="CTA\SearchBundle\Repository\SearchableRepository")
*/
class MyEntity
{
/**
* ...
* This property can be filtered or sorted
*
* @Search\Filterable
* @Search\Sortable
*/
private $id;
/**
* ...
* This property can be searched on or sorted
*
* @Search\Searchable
* @Search\Sortable
*/
private $name;
/**
* ...
* This relation can have its name and description field searched
* and its name and rank field sorted
*
* @Search\Searchable(fields={"name", "description"})
* @Search\Sortable(fields={"name", "rank"})
*/
private $myOtherEntity;
// ...
}
use CTA\SearchBundle\Search\SearchableManager;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\MyEntity;
class Searcher
{
private $searchableManager;
public function __construct(SearchableManager $searchableManager, EntityManagerInterface $entityManager)
{
$this->searchableManager = $searchableManager;
$this->entityManager = $entityManager;
}
public function searchStuff()
{
// Lets search for all the entities using the text search "this is a test" where page size is 20
// and we are trying to get the third page of results and sorting on myOtherEntity rank
$search = $searchableManager->createSearch(MyEntity::class, 20, 40, [], 'this is a test', ['myOtherEntity' => 'ASC']);
$results = $entityManager->getRepository(MyEntity::class)->search($search);
// Get the total number of unpaginated results
$totalCount = $results->getTotalCount();
// Get the entities
$entities = $results->getResults();
}
}
namespace App\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use CTA\SearchBundle\Event\PrepaginationSearchEvent;
class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'my_entity_' . PrepaginationSearchEvent::NAME => 'onSearchPrepagination',
];
}
public function onSearchPrepagination(PrepaginationSearchEvent $event)
{
// Get the alias being used by the query builder for the root entity
$alias = $event->getAlias();
// Get the query builder from the search
$queryBuilder = $event->getQueryBuilder();
// Manipulate
$queryBuilder->andWhere($alias . '.id > 100');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.