How to reduce memory usage - ActiveRecord

Hi,

I want so save about 4000 records to a csv file.

I also want to save related data: author,comment,photo etc.

I’ve made a whlie loop to reduce the memory usage.

Unfortunatelly it did not help, i get the error: ‘Allowed memory size of xxx bytes exhausted (tried…’

My action looks like that:


$allRecords = posts::model()->with('author,comment,photo')->together()->count($criteria);

          

$offset = 0;

$limit = 100;              


header('Content-Disposition: attachment; filename=test.csv');

header('Content-Type: text/x-csv; charset=ISO-8859-1');  


while($offset < $allRecords){

    $criteria->limit = $limit;

    $criteria->offset = $offset;

    $models = posts::model()->with('author,comment,photo')->together()->findAll($criteria);

    $csv = $this->renderPartial('batch/batchCsv', array('models'=>$models), true);

    echo $csv;               

    $offset = $offset+$limit;

    unset($models);

}

What can I do to reduce the memory usage? I use Yii 1.0.x

In 1.0.x there are still some memory leaks in AR. So there are several ways:

  1. Use PHP 5.3 that features garbage collector.

  2. Process your records in multiple runs using console command and cron (not applicable if you are generating CSV on the fly).

  3. Upgrade to Yii 1.1 (probably is too late for up and running project).

  4. Use SQL.

You have to identify only the fields you want to use with ‘select’ clause




$allRecords = posts::model()->with('author,comment,photo')->together()->count($criteria);

          

$offset = 0;

$limit = 100;              


header('Content-Disposition: attachment; filename=test.csv');

header('Content-Type: text/x-csv; charset=ISO-8859-1');  


while($offset < $allRecords){

    $criteria->limit = $limit;

    $criteria->offset = $offset;


	$criteria->select = 'title';

    $models = posts::model()->with(array('author' = array('select' =>'name') ,'comment' => array('select' => 'title'),'photo' => array('select' => 'name')))->together()->findAll($criteria);


    $csv = $this->renderPartial('batch/batchCsv', array('models'=>$models), true);

    echo $csv;               

    $offset = $offset+$limit;

    unset($models);

}