title: How to perform a huge loop in PHP + Doctrine?
body: <p>I'm trying to develop a simple "Ranking" script in PHP using Symfony2 (MongoDB+Doctrine).</p>
  
  <pre><code>    $users = $dm-&gt;createQueryBuilder('Account')
                  -&gt;sort('points', 'DESC')
                  -&gt;getQuery()
                  -&gt;execute();
  
      foreach($users as $user){
              // Do nothing
      }
  </code></pre>
  
  <p>I get the following problem:</p>
  
  <p><strong><em>PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 25 bytes)</em></strong></p>
  
  <p>My database have 80.000 users, so $users have all of them.</p>
  
  <p>Well, I know I can modify php config to allow more memory. And I'm going to do it.
  But I'm looking for other type of solution (apart of adding extra memory to PHP config).</p>
  
  <p>The think is I've tried to develop it with a "pagination" but it doesn't work either (I get the error at user 272, 3rd iteration) :</p>
  
  <pre><code>    $limit = 100;
      $skip = 0;
  
      $total = $dm-&gt;createQueryBuilder('Account')-&gt;count()-&gt;getQuery()-&gt;execute();
      $max = $total/$limit;
  
      while($skip&lt;=$max){
  
          $users = $dm-&gt;createQueryBuilder('Account')
              -&gt;sort('points', 'DESC')
              -&gt;limit($limit)
              -&gt;skip($skip*$limit) // 0x100, 1x100, 2x100, 3x100
              -&gt;getQuery()
              -&gt;execute();
  
          foreach($users as $user){
                 // Do nothing
          }
  
          $skip++;
  
      }
  </code></pre>
  
  <p>Do you have some idea how could I do it?</p>
  
  <p>PS: I think I've done what is suggested in this post: <a href="https://stackoverflow.com/questions/8800978/how-to-handle-huge-select-query-with-symfony-and-doctrine">How to handle huge select query with symfony and doctrine?</a></p>