BabDev / Pagerfanta

Pagination library for PHP applications with support for several data providers
Other
381 stars 170 forks source link

Is there a way to use query builder result cache? #28

Closed fd6130 closed 3 years ago

fd6130 commented 3 years ago

Hi, i'm using fractal + pagerfanta for pagination my result in my project. I wonder how can i apply result cache in my query builder when using pagerfanta?

        $adapter = new QueryAdapter($result);

        $pagerfanta = new Pagerfanta($adapter);

        $pagerfanta->setMaxPerPage($limitPerPage);
        $pagerfanta->setCurrentPage($page);

        $paginatorAdapter = new PagerfantaPaginatorAdapter($pagerfanta, function(int $page) use ($request, $limitPerPage) {
            $route = $request->attributes->get('_route');
            $inputParams = $request->attributes->get('_route_params');
            $newParams = array_merge($inputParams, $request->query->all());
            $newParams['page'] = $page;
            $newParams['limit'] = $limitPerPage;
            return $this->router->generate($route, $newParams, UrlGeneratorInterface::ABSOLUTE_URL);
        });

        $resource = new Collection($pagerfanta->getCurrentPageResults(), $transformer, 'data');
        $resource->setPaginator($paginatorAdapter);