KnpLabs / KnpPaginatorBundle

SEO friendly Symfony paginator to sort and paginate
http://knplabs.com/en/blog/knp-paginator-reborn
MIT License
1.76k stars 341 forks source link

Not showing when using doctrine odm with prime parameter. #326

Open veego opened 9 years ago

veego commented 9 years ago

Hi, KNP paginator is great, but I have one issue with it. If I make query with prime->(true), all working except pagination links is not rendered. Here is my query:

$qb = $dm->createQueryBuilder('AppBundle\Document\Post');
$qb->field('forum')->references($document)
    ->field('author')->prime(true)
    ->sort('created', 'asc');

When I remove ->field('author')->prime(true) it's show again, but it's not good because each author of post need separate db query.

veego commented 9 years ago

I do it other way, pass ArrayCollection of posts to paginator, and in forum document I add custom repository method with prime. Now working, but I dont know wy because there are the same db queries as before.

//Controller
$posts = $forum->getPosts();
...
$paginator->paginate($posts,....

//Document
/**
 * @ODM\ReferenceMany(targetDocument="Post", repositoryMethod="findPosts", simple=true)
 */
protected $posts;
veego commented 9 years ago

After all, not working as expected, sorting very strange, first one user with posts 'asc' then second etc... and Forum document must have array collection with Posts Id's otherwise pager don't show. Switched to pagerfanta, works as expected.

sirian commented 8 years ago

As a workaround you could do reference priming manually. For example add BaseRepository class with method

   public function primeReferences($items, $fields)
    {
        $dm = $this->getDocumentManager();
        $primer = new ReferencePrimer($dm, $dm->getUnitOfWork());
        foreach ($fields as $field) {
            $primer->primeReferences($this->getClassMetadata(), $items, $field);
        }
    }

and then simply call in controller

$repository->primeReferences($pagination->getItems(), ['author']);
amcsi commented 7 years ago

This issue is very relevant: doctrine/mongodb-odm#1283

polc commented 7 years ago

Hello @veego sorry for the late response, has your issue been fixed?

alexseif commented 5 years ago

Hello, I'm currently facing this issue, is there a resolution? Can I somehow provide the count query to the paginator?