Many thanks to joshuapaling. This is a project forked from his repo. He mentions that his plugin is deprecated but I think it was a nice and simple development and I thought to keep it up. I didn't do a pull request to the initial repo because this plugin doesn't follow the CakePHP coding standards and also made a lot of changes to the initial plugin.
A CakePHP 2.x plugin to export/download data as a CSV file. Pass it the result of a $this->MyModel->find('all');
call, and it'll flatten it and download it as a .csv file.
It handles nested belongsTo
associations just fine. As for hasMany
(and other) associations, I don't think they can (or ever need to be) handled gracefully in a single CSV export.
If you think differently, I'm open to suggestions or pull requests.
Tested with CakePHP 2.6. Should work with all CakePHP 2.X.
git submodule add git@github.com:cakeoven/CakeCSV.git app/Plugin/CakeCSV
or download it from https://github.com/cakeoven/CakeCsv
In app/Config/bootstrap.php, add a line to load the plugin:
//Loads only the Csv plugin
CakePlugin::load('CakeCSV');
or //Loads all plugins at once CakePlugin::loadAll();
Add 'CakeCSV.Csv' to your Components array of the relevant controller
var $components = array('CakeCsv.Csv');
Say you had a model/controller for Cities. And say that a City belongsTo a State, which belongsTo a country. Your export function in your Cities controller might look like this:
public function export_cities() {
// It's OK to use containable or recursive in the export data
$this->City->contain(array(
'State' => array(
'Country'
)
));
$data = $this->City->find('all');
$this->Csv->export($data, 'cities.csv');
// a CSV file called cities.csv will be downloaded by the browser.
}
You can set options when loading the component
var $components = [
CakeCsv.Csv => [
'delimiter' => ';',
'enclosure' => '"',
'dataEncoding' => 'UTF-8',
'csvEncoding' => 'WINDOWS-1253',
],
];
You can change this options by accessing the public properties of the component like:
$this->Csv->delimiter = ",";
The export()
function has 2 params:
$data
- an array of data to export. This array should be of the format returned by a call to $this->MyModel->find('all');$fileName
(optional) - the name of the file to download. If blank, it will use a date-stamped name along with the controllers name like export_Cities_2013-09-24.csv
Lets say City belongsTo
State, which belongsTo
country. You might fetch data from the City model looking something like this:
array(
0 => array(
'City' => array(
'name' => 'Sydney',
'population' => '4.6m'
),
'State' => array(
'name' => 'NSW',
'Country' => array(
'name' => 'Australia',
)
)
),
1 => array(
'City' => array(
'name' => 'Melbourne',
'population' => '4.1m'
),
'State' => array(
'name' => 'VIC',
'Country' => array(
'name' => 'Australia',
)
)
),
)
And the Csv component will output a CSV like this:
City.name | City.population | State.name | State.Country.name |
---|---|---|---|
Sydney | 4.6m | NSW | Australia |
Melbourne | 4.1m | VIC | Australia |
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING and CONDUCT for details.
- George Mponos - http://gmponos.webthink.gr
- Joshua Paling - http://www.bbldigital.com.au/
MIT - http://opensource.org/licenses/MIT
Write Unit tests