DDC-3662: "Improve efficiency of One-To-Many EAGER" still do 2 db request when Criteria::$firstResult set #4496

Open
opened 2026-01-22 14:42:57 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Apr 4, 2015).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user fedikw:

In Doctrine 2.5 when I have entity with association One-To-Many and fetch="EAGER", doctrine still do 2 db request , when use Criteria with Criteria::$firstResult and Criteria::$maxResults

-last time I tested on Doctrine 2.5 RC 1, and there this have work perfect: doctrine have use JOIN to load association.-

-I think it get broken between 2.5 rc1 and 2.5-

for load the items I use

$criteria->setFirstResult(0);// this and next, cause 2 request per item
$criteria->setMaxResults(10);
$items = $repositiry->matching($criteria);
Originally created by @doctrinebot on GitHub (Apr 4, 2015). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user fedikw: In Doctrine 2.5 when I have entity with association One-To-Many and fetch="EAGER", doctrine still do 2 db request , when use Criteria with Criteria::$firstResult and Criteria::$maxResults -last time I tested on Doctrine 2.5 RC 1, and there this have work perfect: doctrine have use JOIN to load association.- -I think it get broken between 2.5 rc1 and 2.5- for load the items I use ``` none $criteria->setFirstResult(0);// this and next, cause 2 request per item $criteria->setMaxResults(10); $items = $repositiry->matching($criteria); ```
admin added the Bug label 2026-01-22 14:42:57 +01:00
Author
Owner

@doctrinebot commented on GitHub (Apr 4, 2015):

Comment created by @ocramius:

[~fedikw] could you come up with an example?

This looks like logic that goes through the paginator, if I'm not mistaken...

@doctrinebot commented on GitHub (Apr 4, 2015): Comment created by @ocramius: [~fedikw] could you come up with an example? This looks like logic that goes through the paginator, if I'm not mistaken...
Author
Owner

@doctrinebot commented on GitHub (Apr 5, 2015):

Comment created by fedikw:

Example you have entities: Gallery and Image,
where Gallery have field 'images' that is bidirectional association One-To-Many and fetch="EAGER" with Image,
and you load the galleries using matching($criteria), then:

This code will do 1 db request for load Galleries list + 1 db request for each Gallery item, to load its Images

$repositiry = $em->getRepository('Gallery');
$criteria  = Criteria::create();
$criteria->where(Criteria::expr()->eq("published", 1));
$criteria->setFirstResult(0);
$criteria->setMaxResults(20);

$galleries = $repositiry->matching($criteria);

This code will do 1 db request for load All: Galleries and its Images

$repositiry = $em->getRepository('Gallery');
$criteria  = Criteria::create();
$criteria->where(Criteria::expr()->eq("published", 1));

$galleries = $repositiry->matching($criteria);

I use setFirstResult() and setMaxResults() because I use external pagination not from Doctrine.

After small investigation, I found that it because $this->currentPersisterContext->handlesLimits there e57be9da5e/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php (L1221)

@doctrinebot commented on GitHub (Apr 5, 2015): Comment created by fedikw: Example you have entities: Gallery and Image, where Gallery have field 'images' that is bidirectional association One-To-Many and fetch="EAGER" with Image, and you load the galleries using **matching($criteria)**, then: This code will do 1 db request for load Galleries list + 1 db request for each Gallery item, to load its Images ``` none $repositiry = $em->getRepository('Gallery'); $criteria = Criteria::create(); $criteria->where(Criteria::expr()->eq("published", 1)); $criteria->setFirstResult(0); $criteria->setMaxResults(20); $galleries = $repositiry->matching($criteria); ``` This code will do 1 db request for load All: Galleries and its Images ``` none $repositiry = $em->getRepository('Gallery'); $criteria = Criteria::create(); $criteria->where(Criteria::expr()->eq("published", 1)); $galleries = $repositiry->matching($criteria); ``` I use setFirstResult() and setMaxResults() because I use external pagination not from Doctrine. After small investigation, I found that it because $this->currentPersisterContext->handlesLimits there https://github.com/doctrine/doctrine2/blob/e57be9da5e0c6bb31ac286d213e204784a34aa43/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L1221
Author
Owner

@doctrinebot commented on GitHub (May 13, 2015):

Comment created by fedikw:

seems this issue introduced in that commit a37fa97be3

after I remove

if ((($assoc['type'] & ClassMetadata::TO_MANY) > 0) && $this->currentPersisterContext->handlesLimits) {
      continue;
}

I get 1 DB request, but I not sure which impact it could on other cases :/

@doctrinebot commented on GitHub (May 13, 2015): Comment created by fedikw: seems this issue introduced in that commit https://github.com/doctrine/doctrine2/commit/a37fa97be35e446a84d43c7790d54e2a13e5570b after I remove ``` if ((($assoc['type'] & ClassMetadata::TO_MANY) > 0) && $this->currentPersisterContext->handlesLimits) { continue; } ``` I get 1 DB request, but I not sure which impact it could on other cases :/
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4496