5
votes

I would like to use Doctrine ORM and a pagination method in Symfony. KnpPaginatorBundle looks simple and easy to use. I also use Doctrine repositories because the queries can be easily reused and tested.

This bundle works like others I found. It needs the query builder as parameter. Is there a better way to do this than giving each repository function the paginator as a parameter (or null)? I do not want to paginate every query result so I do not think that the described solution is the best and I am looking for sugestions.

1
In terms of performance, it is better to paginate the results at database level, so you can avoid the overhead of mapping the results to objects, decrease the network traffic and your database server wont need to read/filter that many records. I'm not sure how that bundle works, but you can check what queries get executed and see if the pagination takes place on the query itself. If it does, you are good to go. If you don't want to build the same queries over and over, define them on a repository symfony.com/doc/2.0/book/…ButterDog
I forgot to mention, if you paginate at database level, you will need to count all the records so you can calculate how many pages are there. You could use a cache for this or keep track of the length of your table some other way to avoid this.ButterDog
The bundles paginates at database level using github.com/KnpLabs/knp-componentsRobert Dolca
There is no right answer to this question. We evaluated the options and went with the Pagerfanta/white-october/pagerfanta bundle, which is an alternative option that includes a doctrine orm adapter which accepts either a query builder or a query object, which you can return from your custom repository class method. See: github.com/whiteoctober/Pagerfantagview

1 Answers

2
votes

I know two bundles for doing that:

Both can paginate ORM/Query or ORM/QueryBuilder (and more).