Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count Doctrine's iterate() Collection Result

Having a QueryBuilder Result

$query = $em->createQuery("SELECT....");

Fetching them by the iterate() method

http://doctrine-orm.readthedocs.org/en/2.0.x/reference/batch-processing.html

$objects = $query->iterate();

I am now able to

foreach ($objects as $object) {
    $object = $object[0];
    //do something..
    $object->getObjectId();
    ...
}

BUT...

//after the iterate() call, before to foreach
echo sizeof($objects); //or count($objects);
//always "1", even if i have 10000 foreach loops

Why and how to fix?

like image 715
Daniel W. Avatar asked Feb 07 '13 16:02

Daniel W.


1 Answers

$query->iterate() will only give you an iterator that is not countable. Consider writing a second query that does the COUNT(result) for you, or use the paginator

like image 192
Ocramius Avatar answered Sep 19 '22 00:09

Ocramius