DDC-3661: Doctrine\ORM\LazyCriteriaCollection unpredictable count() #4495

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

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

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user fedikw:

When criteria has set $maxResults Doctrine\ORM\LazyCriteriaCollection::count() can return different result depend when it was called, before initialize() or after.

When LazyCriteriaCollection not initialized then count() return Total from database,
when LazyCriteriaCollection have been initialized then count() return Total from "collection" that is limited by Criteria::$maxResults.

Example you have 30 items in database and Criteria::$maxResults = 5, then:

$items = $repositiry->matching($criteria);
var_dump($items->count()); // will show 30 items
$items->toArray();
var_dump($items->count()); // will show 5 items

It is very confuse ;)

also I not sure which behaviour is right: it should always return Total from database or Total depend from Criteria::$maxResults

Originally created by @doctrinebot on GitHub (Apr 4, 2015). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user fedikw: When criteria has set $maxResults Doctrine\ORM\LazyCriteriaCollection::count() can return different result depend when it was called, before initialize() or after. When LazyCriteriaCollection not initialized then count() return Total from database, when LazyCriteriaCollection have been initialized then count() return Total from "collection" that is limited by Criteria::$maxResults. Example you have 30 items in database and Criteria::$maxResults = 5, then: ``` none $items = $repositiry->matching($criteria); var_dump($items->count()); // will show 30 items $items->toArray(); var_dump($items->count()); // will show 5 items ``` It is very confuse ;) also I not sure which behaviour is right: it should always return Total from database or Total depend from Criteria::$maxResults
admin added the Bug label 2026-01-22 14:42:57 +01:00
Author
Owner

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

@doctrinebot commented on GitHub (Apr 4, 2015): - is required for [DDC-3668: [GH-1372] [DDC-3661] Fix count in Doctrine\ORM\LazyCriteriaCollection](http://www.doctrine-project.org/jira/browse/DDC-3668)
Author
Owner

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

Comment created by @ocramius:

This is indeed a bug: consider writing an example test case so that we can work on it.

@doctrinebot commented on GitHub (Apr 4, 2015): Comment created by @ocramius: This is indeed a bug: consider writing an example test case so that we can work on it.
Author
Owner

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

Comment created by fedikw:

I think I found solution, I try make pull request tomorrow

@doctrinebot commented on GitHub (Apr 4, 2015): Comment created by fedikw: I think I found solution, I try make pull request tomorrow
Author
Owner

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

Comment created by fedikw:

there it is https://github.com/doctrine/doctrine2/pull/1372

-For exaple can take that docs.doctrine-project.org/en/latest/reference/working-with-associations.html#filtering-collections-

For example for User entity:
if you have BirthdayUsers in database 100 and run next code:

$repositiry = $em->getRepository('User');
$criteria = Criteria::create()
    ->where(Criteria::expr()->eq("birthday", "1982-02-17"))
    ->orderBy(array("username" => Criteria::ASC))
    ->setFirstResult(0)
    ->setMaxResults(20)
;
$birthdayUsers = $repositiry->matching($criteria);
var_dump($birthdayUsers->count()); // will show 100 items
$birthdayUsers->toArray();
var_dump($birthdayUsers->count()); // will show 20 items
@doctrinebot commented on GitHub (Apr 5, 2015): Comment created by fedikw: there it is https://github.com/doctrine/doctrine2/pull/1372 -For exaple can take that docs.doctrine-project.org/en/latest/reference/working-with-associations.html#filtering-collections- For example for User entity: if you have BirthdayUsers in database 100 and run next code: ``` none $repositiry = $em->getRepository('User'); $criteria = Criteria::create() ->where(Criteria::expr()->eq("birthday", "1982-02-17")) ->orderBy(array("username" => Criteria::ASC)) ->setFirstResult(0) ->setMaxResults(20) ; $birthdayUsers = $repositiry->matching($criteria); var_dump($birthdayUsers->count()); // will show 100 items $birthdayUsers->toArray(); var_dump($birthdayUsers->count()); // will show 20 items ```
Author
Owner

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

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1372] was labeled:
https://github.com/doctrine/doctrine2/pull/1372

@doctrinebot commented on GitHub (Apr 6, 2015): Comment created by @doctrinebot: A related Github Pull-Request [GH-1372] was labeled: https://github.com/doctrine/doctrine2/pull/1372
Author
Owner

@fracz commented on GitHub (Apr 9, 2018):

This bug just bit me, too.

IMO, this query should take LIMIT & OFFSET into consideration, if set.

@fracz commented on GitHub (Apr 9, 2018): This bug just bit me, too. IMO, [this query](https://github.com/doctrine/doctrine2/blob/c4c59df1b4251f5030a08bd1253a0e472e4bdb7e/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php#L1195-L1197) should take LIMIT & OFFSET into consideration, if set.
Author
Owner

@koteq commented on GitHub (Nov 22, 2018):

Five years ago in a galaxy far, far away someone invented a LazyCriteriaCollection #882.

I wish the behavior to be documented somehow, at least here. It could have saved some hours debugging.

@koteq commented on GitHub (Nov 22, 2018): Five years ago in a galaxy far, far away someone invented a LazyCriteriaCollection #882. I wish the behavior to be documented somehow, [at least here](https://github.com/doctrine/doctrine2/blob/62de42cfa1c2aaddf9fd569e89135fe1ec8f58a3/lib/Doctrine/ORM/LazyCriteriaCollection.php#L37-L41). It could have saved some hours debugging.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4495