PHP code example of giacomoto / symfony-pagination

1. Go to this page and download the library: Download giacomoto/symfony-pagination 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/ */

    

giacomoto / symfony-pagination example snippets




namespace App\Repository;

use App\Entity\User;
use Giacomoto\Bundle\GiacomotoPaginationBundle\Class\PaginatedData;
use Giacomoto\Bundle\GiacomotoPaginationBundle\Class\Pagination;
use Giacomoto\Bundle\GiacomotoPaginationBundle\Interface\IRepositoryHasPagination;
use Giacomoto\Bundle\GiacomotoPaginationBundle\Trait\TRepositoryHasPagination;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class UserRepository extends ServiceEntityRepository implements IRepositoryHasPagination
{
    use TRepositoryHasPagination;

    // Simple pagination
    public function findAllPaginated(Pagination $pagination): PaginatedData
    {
        $data = $this->paginated($pagination, $this->createQueryBuilder('u')
            ->orderBy('u.createdAt', 'ASC'));

        $total = $this->paginatedTotal();

        return new PaginatedData($data, $total);
    }
    
    // With custom QueryBuilder
    public function findAllPaginatedCustom(User $user, Pagination $pagination): PaginatedData
    {
        $queryBuilder1 = $this->createQueryBuilder('u')
            ->where('...');

        $queryBuilder2 = $this->createQueryBuilder('u')
            ->select('count(u.id)')
            ->where('...');

        return new PaginatedData(
            $this->paginated($pagination, $queryBuilder1),
            $this->paginatedTotal($queryBuilder2)
        );
    }
}

class PaginatedData {
    public function __construct(
        private readonly array $data,
        private readonly int $total,
    )
    {
    }

    /**
     * @return array
     */
    public function getData(): array
    {
        return $this->data;
    }

    /**
     * @return int
     */
    public function getTotal(): int
    {
        return $this->total;
    }
}

use Giacomoto\Bundle\GiacomotoPaginationBundle\Class\Pagination;
use Giacomoto\Bundle\GiacomotoPaginationBundle\Service\PaginationService;

class UsersController extends AbstractController
{
    public function getAll(
        Request           $request,
        userRepository    $userRepository
        PaginationService $paginationService,
    ): JsonResponse
    {
        // PaginationService will create for you the Pagination class form the http Request
        // Params are passed in the query url and are "page" and "size", both optional
        $pagination = $paginationService->createFromRequest($request);

        // Alternatively you can create the Pagination class manually
        // $pagination = new Pagination(1 /*page*/, 5 /*size*/);
        
        // Get PaginatedData
        $paginated = $userRepository->findAllPaginated($pagination); 
    
        // ...
    }
}
Repository/UserRepository.php