KnpPaginatorBundle
KnpPaginatorBundle copied to clipboard
Not showing when using doctrine odm with prime parameter.
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.
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;
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.
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']);
This issue is very relevant: doctrine/mongodb-odm#1283
Hello @veego sorry for the late response, has your issue been fixed?
Hello, I'm currently facing this issue, is there a resolution? Can I somehow provide the count query to the paginator?