This recipe shows how to parse comma-separated values (CSV) files using a datasource, showing a clean approach to CSV processing.
We start by installing CakePHP's datasources plugin. Download the latest release from http://github.com/mariano/datasources/downloads and uncompress the downloaded file into your app/plugins
folder. You should now have a directory named datasources
inside app/plugins
.
The datasources plugin, located at http://github.com/cakephp/datasources, is an official CakePHP plugin that offers several community-provided datasources, such as XML-RPC and SOAP. This and other recipes use a customized version of the plugin, modified for the purpose of this book.
We need some sample data to work with. Create a file named contacts.csv
and place it in a folder of your choice (such as /home/mariano
), with contents similar to the ones shown below. This example includes only two rows of data, but the file used in this recipe uses several...