Book Image

CakePHP 2 Application Cookbook - Third Edition

By : Watts
Book Image

CakePHP 2 Application Cookbook - Third Edition

By: Watts

Overview of this book

If you are a CakePHP developer looking to ease the burden of development, then this book is for you. As a headfirst dive into the framework, this collection of recipes will help you get the most out of CakePHP, and get your applications baked in no time. Even if you're not familiar with the framework, we'll take you from basic CRUD building to useful solutions that will aid in getting the job done quickly and efficiently.
Table of Contents (14 chapters)
13
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...