EzDevInfo.com

Pagerfanta

Pagination for PHP.

Pagerfanta reverse order pagination

My question is : is it possible to easily make WhiteOctoberPagerFantaBundle for SF2 in reverse order?

I mean, when we normally have pagination: 1, 2, 3 (newest content at the 1 page) -> to make : 241,240,239,.... ( newest content at the 241).

Thank you.


Source: (StackOverflow)

Undefined method Jackalope\Query\QueryResult::count() when accessing Sylius's 'Blocks' or 'Pages' section on backend

When I click on the 'Blocks' or 'Pages' section in the sidebar in Sylius's admin backend, I get the following error:

FatalErrorException: Error: Call to undefined method Jackalope\Query\QueryResult::count() in /Users/sabrinaliao/Sites/Symfony2/Development/SmartSpine/vendor/pagerfanta/pagerfanta/src/Pagerfanta/Adapter/DoctrineODMPhpcrAdapter.php line 51

I made sure my composer.json matched Sylius's (link). The only difference is that I also added requirements for a few sensio bundles I needed (namely the framework-extra-bundle and the generator-bundle). I looked at the most recent files for jackalope's QueryResult and Pagerfanta's DoctrineODMPhpcrAdapter to ensure that my files were up-to-date. Just to be sure, I manually removed all files in my vendor directory and reran composer.phar update. The error persisted. I looked at Sylius-Standard's composer.json and tried changing my jackalope version to the one shown there, but that just gave me the error about requesting an uninstallable set of packages. I don't know if this is a Sylius problem or a Jackalope problem or a Pagerfanta problem. Would anyone be able to help?


Source: (StackOverflow)

Advertisements

pagerfanta used in a form with a search. symfony2

I'd like the paging link render by pagerfanta be submitted with the form data. This is to consider the data entered in the search form.

A simple pagination link will not allow me to navigate in my search result. Any help please ?

controller

/**
 * @Route("/{page}/", name="admin_user",requirements={"page" = "\d+"}, defaults={"page" = 1})
 * @Template()
 */
public function indexAction($page = 1)
{

    $data = [];
   $data['name'] = $this->getUser()->getName();
    $form = $this->createForm('admin_user_search_type', null);
    $request = $this->getRequest();

    if ($request->getMethod() == 'POST') {
        $form->bind($request);
        $data = array_merge($data, $form->getData());
    }

    return $this->render('AABundle:User:index.html.twig', array(
                'pager' => $this->getDoctrine()->getManager()->getRepoitory('AABundle:User')->search($data, 4, $page, $this->getUser()),
                'form' => $form->createView(),
    ));
}

view.html.twig

{% if pager.haveToPaginate %}
        {{ pagerfanta(pager, 'twitter_bootstrap3') }}
    {% endif %}

Source: (StackOverflow)

Symfony2 Pagerfanta on ajax

I want to paginate data from ajax with Pagerfanta. I have the next button displayed and I get as many results per page as I want (currently set to1). The problem is that after clicking next, another results don't show and I can't figure out why.

Here is a code snippet from Controller:

$pager = new Pagerfanta(new ArrayAdapter($offers));
$pager->setMaxPerPage(1);
$pager->setCurrentPage(1);
(...)
'offers'         => $pager->getCurrentPageResults(),
'pager'          => $pager

ajax part (I'm passing initial page number here, is it correct way?)

url: "{{ path('page_ajax_search', {'page' : 1}) }}",

and here is twig:

{% if pager.haveToPaginate %}
<div class="pagerfanta">
    {{ pagerfanta(pager, 'twitter_bootstrap3') }}
</div>
{% endif %}

Any suggestions?


Source: (StackOverflow)

Use elastica with PagerFanta and dynamic querybuilder

I have an application with symfony2 / doctrine 2 / elastica / fosElasticaBundle / pagerFanta.

I want to use a custom and dynamic queryBuilder in combination with pagerfanta and elastica. Not to transform the results but to prefilter them.

So far I was able to : 1. Use pagerfanta by itself with my custom queryBuilder :

    $page = $request->get('page', 1);
    $search = $request->get('search');
    $querybuilder = $this->getDoctrine()->getRepository('AppBundle:FoodAnalytics\Recipe')->findByTopCategoryQueryBuilder($category);
    $explorerManager = $this->get('explorer_manager');
    $pagerFanta = $explorerManager->getPagerFanta($querybuilder, $page, 4);
    $recipes = $pagerFanta->getCurrentPageResults();
  1. Use Elastica with Pagerfanta but without my custom QueryBuilder :

    $page = $request->get('page', 1);
    $search = $request->get('search');
    $finder = $this->container->get('fos_elastica.finder.website.recipe');
    $pagerFanta = $finder->findPaginated($search);
    $recipes = $pagerFanta->getCurrentPageResults();
    

Now, how can I also use my custom QueryBuilder ? I know you can set a custom one in elastica config but mine has to be dynamic = take an argument, so I'd like to set it in the controller. Is that possible ?


Source: (StackOverflow)

Symfony2, PagerFanta results

How can I take a result as an array from PagerFanta in Symfony2.1.1 ?

        $adapter = new \Pagerfanta\Adapter\DoctrineORMAdapter($query);
        $pager = new \Pagerfanta\Pagerfanta($adapter);
        $pager->setMaxPerPage(45);
        $data = $pager->getCurrentPageResults();

Results of print_r($data);

ArrayIterator Object
(
    [storage:ArrayIterator:private] => Array
        (
            [0] => Trucking\MainBundle\Entity\Sparcs Object
                (
                    [id:Trucking\MainBundle\Entity\Sparcs:private] => 77940
                    [container:Trucking\MainBundle\Entity\Sparcs:private] => MEDUUUU
                    ...
                    ...
                    ...

I want to get results as getQuery->getArrayResult();


Source: (StackOverflow)

Symfony2 pagerfanta DQL with leftjoin

I'm trying to paginate a query that uses DQL (because I'm going to use some group functions like sum) and has an outer join, here a simplified code:

//in controller
use
Pagerfanta\Pagerfanta,
Pagerfanta\Adapter\DoctrineORMAdapter as PagerAdapter;

//in list action
$query=$em->createQuery(
   'select m,sum(d.amount) from 
   ABundle:Master m
   left join ABundle:Detail d with d.master=m
   group by m.date'
);

$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, true);
$paginator=new Pagerfanta(new PagerAdapter($query));
$paginator->setMaxPerPage($this->getPerPage());
$paginator->setCurrentPage($this->getPage(),false,true);

return $paginator;

the problem is I get the following error:

An exception has been thrown during the rendering of a template ("Cannot count query which selects two FROM components, cannot make distinction")

I've been trying to set a hint called HINT_CUSTOM_OUTPUT_WALKER so that pagerfanta uses my query as a view and runs its own count but I haven't been able to solve this.

Can anyone please give me some guidance about how to properly use it?


Source: (StackOverflow)

What is correct way to use Pagerfanta for pagination in Symfony2

I have read that Pagerfanta is the pagination plugin of choice for Symfony2, but I am having some trouble getting it to work correctly. I have downloaded the code for pagerfanta and PagerfantaBundle and installed them as described in the WhiteOctober Github readme file and in this tutorial. I'm pretty sure the code in my controller is okay, but there is something wrong with my template file.

My controller code:

public function indexAction($page =  null)
{
    ...
    $query = $em->createQuery('SELECT something FROM some_table');
    $adapter = new DoctrineORMAdapter($query);
    $pager =  new Pagerfanta($adapter);
    $pager->setMaxPerPage(10);
    if (!$page)    $page = 1;
    try  {
        $pager->setCurrentPage($page);
    }
    catch(NotValidCurrentPageException $e) {
      throw new NotFoundHttpException('Illegal page');
    }
    return $this->render('MyBundle:MyEntity:index.html.twig', array(
        'pager' => $pager,
    ));
}

In my template:

...
<table>
  {% for object in pager.currentPageResults %}
    <tr>
      <td>{{ object.attribute1 }}</td>
      <td>{{ object.attribute2 }}</td>
      <td>{{ object.attribute3 }}</td>
    </tr>
  {% endfor %}
</table>

{% if pager.haveToPaginate %}
  {{ pagerfanta(pager) }}
{% endif %}

When I go to the page, I get a list of the first 10 objects. However,the rendering of the pager is just PreviousNext as one word (not links and no pages 1, 2, 3, etc).

If I add ?page=2 to the URL, it doesn't display the next 10 items in the list.

I didn't change the route from what is was before I added the pagination code because the documentation says:

The routes are generated automatically for the current route using the variable "page" to propagate the page number.

... but I wonder if incorrect routing is part of the problem. Assuming this is the case, I'm not sure how to proceed with this.

I have been unable to find any instructions other the sources I have linked to above, so it would be great if someone who has implemented this successfully could share the details of how they did it.


Source: (StackOverflow)

Pagerfanta and Doctrine2 COUNT optimization

I'm using Pagerfanta and Doctrine Adapters with Symfony2 and Silex. As my database became bigger I noticed huge load on admin stats pages that display big data with pagination. I checked profiler and saw unbelievably inefficient queries:

SELECT DISTINCT id16
FROM (
    SELECT f0_.username AS username0, ..., f0_.added_on AS added_on20
    FROM fos_user f0_ ORDER BY f0_.id DESC
) dctrn_result
LIMIT 50 OFFSET 0;

SELECT COUNT(*) AS dctrn_count
FROM (
    SELECT f0_.username AS username0, ..., f0_.added_on AS added_on20
    FROM fos_user f0_ ORDER BY f0_.id DESC
) dctrn_result
LIMIT 50 OFFSET 0;`

First query was easy to fix by creating fixed version of DoctrineORMAdapter class. The code that generates COUNT() query is more complicated so I decided to ask if there's any solution for this.

So is there any way to make Pagerfanta not running nested queries?


Source: (StackOverflow)

Check permission on paginated overview per entity

I'm using voters to check if a user has the correct permissions to perform a certain action on a entity.

CRUD actions are easy to check. But how do I check the permissions on result sets or overviews. The overviews use pagination with PagerFanta to paginate the results. Checking the results beforehand won't be possible because of performance issues. Only checking the results which have been return in pagination could lead to empty or half empty pages.

I'm thinking of putting the same validation in my repository so they only return results the users is allowed to see. But this creates code duplication because the same validation is now duplicated, once in a voter and once in a repository.

Is there a better solution to this or aren't voters the best solution for this?


Source: (StackOverflow)

pagerfanta submit with the form data

I have a form with criterias that return a list of results in the same view adding the PagerFanta paginator. To go to next page or previous page I need send the same form data (criterias).

What is the better way?

Thank you and I'm sorry for my English.


Source: (StackOverflow)

pagerfanta - DoctrineORMAdapter Sorting

Although I know this is trivial I'm stuck trying to implement the pagerfanta Paginator using the DoctrineORMAdapter, I want to paginate all entities sorted by id in descending order, the final SQL I want is this:

SELECT id, name FROM User ORDER BY id DESC LIMIT 0, 5;

Suppose I had users from A to Z and I want to limit them by 5 each page, what DoctrineORMAdapter is paginating results in User E to A listed in the first page, while what I actually expect is to see User Z to user V in the first page, U to Q in the second page and so on. The DQL I'm passing to DoctrineORMAdapter is as follow:

SELECT u FROM My\FluffyBundle\Entity\User u ORDER BY u.id DESC

On execution this is the final DQL for the first page:

SELECT DISTINCT id0 FROM (SELECT u0_.id AS id0, u0_.name AS name1 FROM User u0_ 
ORDER BY u0_.id DESC) dctrn_result LIMIT 5 OFFSET 0

Please note that when using the ArrayAdapter instead of DoctrineORM's it works as expected, but it's not a good idea to rely on ArrayAdapter when you have thousands of complex Doctrine Entities, not even with extra lazy loading :D.

This is the only relevant code:

$queryBuilder = $repo->createQueryBuilder('u')->orderBy('u.id', 'DESC');
$adapter = new DoctrineORMAdapter($queryBuilder);
$pager = new Pagerfanta($adapter);
$pager->setMaxPerPage(5);

Thanks.


Source: (StackOverflow)