DDC-1730: setMaxResults() does not work properly #2176

Closed
opened 2026-01-22 13:43:22 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Mar 28, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user nikola.petkanski:

In my understanding ORM is Object-relational mapping, so it is fare to say that setting the maximum results for a given query should return exactly this many objects mapped.

example case 1:

$query = "SELECT a, t FROM SomeBundle:Articles a JOIN a.translations t";
$query = $this->getEntityManager()->createQuery($dql);
$query->setMaxResults(1);

One would expect one Article with multiple translations attached. Unfortunately, this is not the case - you have your article, but only one translation attached.

example case 2:

$query = "SELECT a, t FROM SomeBundle:Articles a JOIN a.translations t";
$query = $this->getEntityManager()->createQuery($dql);
$query->setMaxResults(5);

In this case one would expect exactly 5 Articles to be returned with all their translations attached. Unfortunately, this might or might not be the case - depending on the order of the data returned by the database engine.

I am aware that this is because of the limit clause of the SQL, however, note that I'm setting max result to the Query object, which should have been processed by the EntityManager.

I can't think of a solution for this case, but a proper ORM should take care of such cases.

Yes, I know I can leave this one to the lazy loading and it would work, but I happened to have a case where I have multiple (>200) results and each one of them having multiple relations. In such case having a query per each entity is not an option.

Originally created by @doctrinebot on GitHub (Mar 28, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user nikola.petkanski: In my understanding ORM is Object-relational mapping, so it is fare to say that setting the maximum results for a given query should return exactly this many objects mapped. example case 1: ``` $query = "SELECT a, t FROM SomeBundle:Articles a JOIN a.translations t"; $query = $this->getEntityManager()->createQuery($dql); $query->setMaxResults(1); ``` One would expect one Article with multiple translations attached. Unfortunately, this is not the case - you have your article, but only one translation attached. example case 2: ``` $query = "SELECT a, t FROM SomeBundle:Articles a JOIN a.translations t"; $query = $this->getEntityManager()->createQuery($dql); $query->setMaxResults(5); ``` In this case one would expect exactly 5 Articles to be returned with all their translations attached. Unfortunately, this might or might not be the case - depending on the order of the data returned by the database engine. I am aware that this is because of the limit clause of the SQL, however, note that I'm setting max result to the Query object, which should have been processed by the EntityManager. I can't think of a solution for this case, but a proper ORM should take care of such cases. _Yes, I know I can leave this one to the lazy loading and it would work, but I happened to have a case where I have multiple (>200) results and each one of them having multiple relations. In such case having a query per each entity is not an option._
admin added the Bug label 2026-01-22 13:43:22 +01:00
admin closed this issue 2026-01-22 13:43:22 +01:00
Author
Owner

@doctrinebot commented on GitHub (Mar 30, 2012):

Comment created by @beberlei:

Doctrine 2.2 has a paginator library that fixes this. In context of AbstractQuery (Native orDQL) setMaxResults() is always SQL based, never object based however.

@doctrinebot commented on GitHub (Mar 30, 2012): Comment created by @beberlei: Doctrine 2.2 has a paginator library that fixes this. In context of AbstractQuery (Native orDQL) setMaxResults() is always SQL based, never object based however.
Author
Owner

@doctrinebot commented on GitHub (Mar 30, 2012):

Issue was closed with resolution "Won't Fix"

@doctrinebot commented on GitHub (Mar 30, 2012): Issue was closed with resolution "Won't Fix"
Author
Owner

@aistis- commented on GitHub (Jul 18, 2017):

I guess a quick work around:

...

foreach ((new Paginator($qb->getQuery(), false))->getIterator() as $object) {
    return $object;
}

return null;
@aistis- commented on GitHub (Jul 18, 2017): I guess a quick work around: ```php ... foreach ((new Paginator($qb->getQuery(), false))->getIterator() as $object) { return $object; } return null; ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2176