1. Go to this page and download the library: Download ttskch/pagerfanta-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/ */
// FooCriteria.php
use Ttskch\PagerfantaBundle\Entity\Criteria;
class FooCriteria extends Criteria
{
public $query;
}
// FooSearchType.php
use Symfony\Component\Form\Extension\Core\Type\SearchType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Ttskch\PagerfantaBundle\Form\CriteriaType;
class FooSearchType extends CriteriaType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('query', SearchType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => FooCriteria::class,
// if your app depends on symfony/security-csrf adding below is recommended
// 'csrf_protection' => false,
]);
}
}
// FooRepository.php
public function createQueryBuilderFromCriteria(FooCriteria $criteria)
{
return $this->createQueryBuilder('f')
->where('f.name like :query')
->orWhere('f.email like :query')
->setParameter('query', sprintf('%%%s%%', str_replace('%', '\%', $criteria->query)))
->orderBy(sprintf('f.%s', $criteria->sort), $criteria->direction)
;
}
// FooController.php
public function index(FooRepository $fooRepository, Context $context)
{
$context->initialize('id', FooCriteria::class, FooSearchType::class);
$queryBuilder = $fooRepository->createQueryBuilderFromCriteria($context->criteria);
$adapter = new DoctrineORMAdapter($queryBuilder);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta
->setMaxPerPage($context->criteria->limit)
->setCurrentPage($context->criteria->page)
;
return $this->render('index.html.twig', [
'form' => $context->form->createView(),
'pagerfanta' => $pagerfanta,
]);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.