Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/proloser/cakephp-csv
A component that will import/export data from a csv file into a save-friendly nested model data array format.
https://github.com/proloser/cakephp-csv
Last synced: 9 days ago
JSON representation
A component that will import/export data from a csv file into a save-friendly nested model data array format.
- Host: GitHub
- URL: https://github.com/proloser/cakephp-csv
- Owner: ProLoser
- License: mit
- Created: 2010-01-15T03:23:33.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2019-01-08T07:11:26.000Z (almost 6 years ago)
- Last Synced: 2024-08-10T11:26:04.028Z (3 months ago)
- Language: PHP
- Homepage:
- Size: 38.1 KB
- Stars: 52
- Watchers: 8
- Forks: 42
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# CSV Plugin
Allows the importing and exporting of a standard $this->data formatted array to and from csv files.
Doesn't currently support HABTM.## Options
Importing, exporting and setup come with the same options and default values
```php
$options = array(
// Refer to php.net fgetcsv for more information
'length' => 0,
'delimiter' => ',',
'enclosure' => '"',
'escape' => '\\',
// Generates a Model.field headings row from the csv file
'headers' => true,
// If true, String $content is the data, not a path to the file
'text' => false,
)
```## Instructions
* Add Behavior to the table
```php
addBehavior('CakePHPCSV.Csv', $options);
}
}
?>
```### Importing
* Upload a csv file to the server
* Import the csv file into your data variable:
**Approach 1:** Use a CSV file with the first row being Model.field headers
```php
Posts.csv
Post.title, Post.created, Post.modified, body, user_id, Section.name, Category.0.name, Category.0.description, Category.1.name, Category.1.description
..., ..., ...
``````php
$this->data = $this->Posts->import($content, $options);
```**Approach 2:** Pass an array of fields (in order) to the method
```php
$data = $this->Posts->import($content, array('Post.title', 'Post.created', 'Post.modified', 'body', 'user_id', 'Category.0.name', 'Category.0.description', 'Category.1.name', 'Category.1.description'));
```* Process/save/whatever with the data
```php
$entities = $this->Posts->newEntities($data);
$Table = $this->Posts;
$Table->connection()->transactional(function () use ($Table, $entities) {
foreach ($entities as $entity) {
$Table->save($entity, ['atomic' => false]);
}
});
```### Exporting
* Populate an $this->data type array
```php
$data = $this->Post->find()->all();
```* Export to a file in a writeable directory
```php
$this->Posts->exportCsv($filepath, $data, $options);
```### Additional optional callbacks:
* `beforeImportCsv($filename, $fields, $options)` returns boolean $continue
* `afterImportCsv($data)`
* `beforeExportCsv($filename, $data, $options)` returns boolean $continue
* `afterExportCsv()`### FAQ
#### Incorrect Line Endings (OSX)
Some people [have mentioned](https://github.com/ProLoser/CakePHP-CSV/issues/6) having incorrect line endings. This can be fixed by having this in your php codebase:
```
ini_set("auto_detect_line_endings", true);
```