Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
doctrinebot opened this issue Apr 4, 2015 · 7 comments
Open
Assignees
Labels

Comments

@doctrinebot
Copy link

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

@doctrinebot
Copy link
Author

@doctrinebot
Copy link
Author

Comment created by @Ocramius:

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

@doctrinebot
Copy link
Author

Comment created by fedikw:

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

@doctrinebot
Copy link
Author

Comment created by fedikw:

there it is #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
Copy link
Author

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1372] was labeled:
#1372

@fracz
Copy link

fracz commented Apr 9, 2018

This bug just bit me, too.

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

fracz added a commit to SUPLA/supla-cloud that referenced this issue Apr 9, 2018
Related bug that caused inappropriate behavior: doctrine/orm#4491
fracz added a commit to SUPLA/supla-cloud that referenced this issue Apr 9, 2018
Related bug that caused inappropriate behavior: doctrine/orm#4491
@koteq
Copy link

koteq commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants