Book Image

CakePHP 2 Application Cookbook

Book Image

CakePHP 2 Application Cookbook

Overview of this book

Table of Contents (20 chapters)
CakePHP 2 Application Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Import parser


One benefit of using shells in CakePHP is to handle process-intensive tasks, which take a long time to run. In this recipe, we'll create a CakePHP console shell to deal with a mass CSV data import. We'll process a data file and import all rows, while logging import errors to another file.

Getting ready

As this is the only task in our shell, we're not using Task this time.

First, if you don't have it already, create the packages database table using the following SQL statement:

CREATE TABLE packages (
  id INT NOT NULL AUTO_INCREMENT,
  recipient VARCHAR(255) NOT NULL,
  address VARCHAR(255) NOT NULL,
  created DATETIME,
  modified DATETIME,
  PRIMARY KEY(id)
);

Then, create the associated model in a file named Package.php in app/Model/ with the following content:

<?php
App::uses('AppModel', 'Model');

class Package extends AppModel {
}

How to do it...

Perform the following steps:

  1. Create a file named ImportShell.php in app/Console/Command/ to import CSV files with the following code...