2017 © Pedro Peláez
 

cakephp-plugin cakephp-csv

image

dario86/cakephp-csv

  • Saturday, May 7, 2016
  • by dario86
  • Repository
  • 1 Watchers
  • 0 Stars
  • 47 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 43 Forks
  • 0 Open issues
  • 3 Versions
  • 12 % Grown

The README.md

CSV Plugin

Allows the importing and exporting of a standard $this->data formatted array to and from csv files. Doesn't currently support HABTM., (*1)

Options

Importing, exporting and setup come with the same options and default values, (*2)

$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,
        'array' => false
)

Instructions

  • Add Behavior to the table
addBehavior('CakePHPCSV.Csv', $options);
    }
}
?>

Importing

  • Upload a csv file to the server, (*3)

  • Import the csv file into your data variable:, (*4)

Approach 1: Use a CSV file with the first row being Model.field headers, (*5)

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
..., ..., ...
$this->data = $this->Posts->import($content, $options);

Approach 2: Pass an array of fields (in order) to the method, (*6)

$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
$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

Approach 1: Entity, (*7)

  • Populate a $this->data type entity
$data = $this->Post->find()->all();
  • Export to a file in a writeable directory
$this->Posts->exportCsv($filepath, $data, $options);

Approach 2: Array, (*8)

  • Populate an $this->data type array
$data = $this->Post->find()->toArray();
  • Export to a file in a writeable directory. In config, set option array to true
$options['array'] = true;
$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 having incorrect line endings. This can be fixed by having this in your php codebase:, (*9)

ini_set("auto_detect_line_endings", true);

The Versions

07/05 2016

dev-master

9999999-dev

  Sources   Download

The Requires

 

The Development Requires

24/09 2015

dev-cake2

dev-cake2

  Sources   Download

The Requires

 

05/05 2015

dev-cake3

dev-cake3

  Sources   Download

The Requires

 

The Development Requires