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

Injecting data

Sometimes you need to save initial data for your newly created tables or ensure that old data is consistent with one of your migrations right after you applied it.

The CakeDC Migrations plugin provides a way to alter your database contents while running a migration, so you can execute custom logic before or after a given migration. This custom code can be used to insert or update data in your database to keep your changes consistent.

Getting ready

In this recipe, we'll add a new feature to our blog which involves setting up a unique group for our profiles. We'll want to allow groups to be dynamic, so a new groups table and Group model will be created and associated with our Profile model.

We'll first create a table for our groups using the following SQL statement:

CREATE TABLE groups (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  created DATETIME,
  modified DATETIME,
  PRIMARY KEY(id)
);

Now, we will use our profiles table created earlier (shown...