title: Iterate from a Doctrine_Query
body: <p>I am using Doctrine 1.2 in a symfony project.</p>
  
  <p>I would like to iterate with doctrine, I have a Doctrine_Query object, I want to iterate on the "result set" of this query because I can have 100K records and don't want to run out of memory.</p>
  
  <p>Something like : </p>
  
  <pre><code>$it = $query-&gt;iterate();
  while($it-&gt;next()) {
    $obj = $it-&gt;getOjbect();
    //Do some stuff with this object
  }
  </code></pre>
  
  <p>I figured out how todo by using a custom hydrator.</p>
  
  <p>For a symfony project, in ProjectConfiguration.class.php :</p>
  
  <pre><code>public function configureDoctrine(Doctrine_Manager $manager)
  {
    $manager-&gt;registerHydrator('my_hydrator', 'Doctrine_Hydrator_MyHydrator');
  }
  </code></pre>
  
  <p>In a controller : </p>
  
  <pre><code>$this-&gt;filename = $query-&gt;execute(array(), 'my_hydrator');
  
  Added in lib folder : 
  
  class Doctrine_Hydrator_MyHydrator extends Doctrine_Hydrator_Abstract
  {
      public function hydrateResultSet($stmt)
      {
        $filename = sfConfig::get('sf_data_dir').'/'.time().'.csv';
        $fh = fopen($filename,'w');
        while($f = $stmt-&gt;fetch(Doctrine_Core::FETCH_BOTH)){
          $f = array(
              $f[0],
              $f[1],
              $f[2],
              $f[3],
              $f[4],
              $f[5],
              $f[6],
              $f[7],
              $f[8],
          );
          fputcsv($fh, $f); 
        }
        fclose($fh); 
        return $filename;
      }
  </code></pre>