Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

CsvExportBehavior

joncutrer edited this page Feb 29, 2012 · 5 revisions

is part of ProUtils plugin for CakePHP 2

Adds the exportCSV method to the model, exports all records.

You can configure the CsvExport behavior using these options:

  • delimiter - The delimiter for the values, default is ;
  • enclosure - The enclosure, default is "
  • encoding - utf8 or default encoding, default is utf8
  • max_execution_time - Extend the execution time for larger datasets, default is 360

The main method of this behavior is

$csvdata = $this->Model->exportCSV();

Enable within your model

public $actsAs = array( 'ProUtils.CsvExport' );

Export data and present as a file download from your controller

function export() {
    $this->autoRender = false;
    $modelClass = $this->modelClass;
    $this->response->type('Content-Type: text/csv');
    $this->response->download( strtolower( Inflector::pluralize( $modelClass ) ) . '.csv' );
    $this->response->body( $this->$modelClass->exportCSV() );
}

CsvExportBehavior is part of ProUtils plugin for CakePHP 2

Clone this wiki locally