KnpPaginatorBundle icon indicating copy to clipboard operation
KnpPaginatorBundle copied to clipboard

Not showing when using doctrine odm with prime parameter.

Open veego opened this issue 9 years ago • 6 comments

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 avatar Aug 26 '15 18:08 veego

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 avatar Aug 26 '15 23:08 veego

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.

veego avatar Aug 27 '15 14:08 veego

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']);

sirian avatar Dec 29 '15 14:12 sirian

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

amcsi avatar Jul 18 '17 13:07 amcsi

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

polc avatar Sep 15 '17 12:09 polc

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

alexseif avatar Jul 10 '19 15:07 alexseif