How to limit size of result set in doctrine 2?

PhpOrmDoctrine Orm

Php Problem Overview


If i'm using the findBy method of the respository class, how can I limit the size of the result set?

Php Solutions


Solution 1 - Php

In Doctrine 2.1 method EntityRepository#findBy() now accepts additional parameters for ordering, limit and offset.

see full list new features in doctrine 2.1 (404) Relevant link to findBy and findOneBy

example:

 public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)

usage:

$product = $repository->findBy(
    array('name' => 'foo'),
    array('price' => 'ASC'),
    $myLimit,
    $myOffset
);

Solution 2 - Php

Solution 3 - Php

The findBy() method of the generic repository class doesn't support this.

I would write your own repository (as outlined here) and override findBy() to take additional parameters. Your new implementation could use the query builder, or plain-old-DQL to build up the proper query. (I'd use the querybuilder, as you can probably just pass the $critera param right into QueryBuilder::where())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionblacktie24View Question on Stackoverflow
Solution 1 - PhpNikolai SenkevichView Answer on Stackoverflow
Solution 2 - PhpJarzonView Answer on Stackoverflow
Solution 3 - PhptimdevView Answer on Stackoverflow