Search code examples
cakephpcsvimportpreview

CakePHP CSV Import with Preview


There are many usefull CSV-Importer, but i didn't found one with a "Preview" of the Data to import. So start to make my own: but i stuck on HOW to organise all this in a nice cake-like way (just started programming)

Functions

This importer should be/have/can:

  1. easy attached to every controller (and set a default model, so the tool can be called with /url/controller/csv-importer
  2. after accessing this csv-importer via URL, you see a pre-defined view of this importer with an upload form for the CSV, next step let you
  3. choose delimiter, charset, primaryKey to sync with (those options can be preconfigured on loading the importer, so those options (part 3) would be skipped)
  4. show the PREVIEW of data from the uploaded CSV, which record would be inserted, which record would be changed
  5. you can select the rows which should be imported and IMPORT, or ABORT and re-upload the csv

Problem

how should a organise this to offer this as a tool, which can easy be accessed by an user from everywhere (where i added this importer)

i don't need code :) it's all about how to organise this.


Solution

  • The official Datasource repository contains a CSV Datasource https://github.com/cakephp/datasources/tree/2.0/

    Doing this in a component is wrong in the MVC pattern. Data processing is a model task for obvious reasons like re-usability in a shell. And models are easier to unit test.

    I don't understand how you intend to use this in every controller. If you need to display the uploaded CSV as a table on every page then simply do a requestAction() simply fetch the CSV data using the datasource I've told you from the model where needed and set it to the view.