Scaffolding with the command-line tool

Exclusive offer: get 50% off this eBook here
Instant CakePHP Starter [Instant]

Instant CakePHP Starter [Instant] — Save 50%

Learn everything you need to develop a feature-rich CakePHP app, from installation to deployment with this book and ebook

$12.99    $6.50
by Mark Robert Henderson | July 2013 | Open Source PHP Web Development

In this article created by Mark Robert Henderson, the author of Instant CakePHP Starter, we will discuss the tool schema and its database.

(For more resources related to this topic, see here.)

CakePHP comes packaged with the Cake command-line tool, which provides a number of code generation tools for creating models, controllers, views, data fixtures, and more, all on the fly.

Please note that this is great for prototyping, but is non-ideal for a production environment.

On your command line, from the cake-starter folder, type the following:

cd app
Console\cake bake

You will see something similar to the following:

> Console/cake bake
Welcome to CakePHP v2.2.3 Console
---------------------------------------------------------------
App : app
Path: /path/to/app/
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q)
>

As you can see, there's a lot to be done with this tool. Note that there are other commands beside bake, such as schema, which we be our main focus in this article.

Creating the schema definition

Inside the app/Config/Schema folder, create a file called glossary.php. Insert the following code into this file:

<?php
/**
* This schema provides the definitions for the core tables in
the glossary app.
*
* @var $glossary_terms - The main terms/definition table for the app
* @var $categories - The categories table
* @var $terms_categories - The lookup table, no model will
be created.
*
* @author mhenderson
*
*/
class GlossarySchema extends CakeSchema {
public $glossaryterms = array(
'id' => array('type' => 'integer', 'null' => false,
'key' => 'primary'),
'title' => array('type' => 'string', 'null' => false,
'length' => 100),
'definition' => array('type' => 'string', 'null' =>
false, 'length' => 512)
);
public $categories = array(
'id' => array('type' => 'integer', 'null' => false,
'key' => 'primary'),
'name' => array('type' => 'string', 'null' => false,
'length' => 100),
'definition' => array('type' => 'string', 'null' =>
false, 'length' => 512)
);
public $glossaryterms_categories = array(
'id' => array('type' => 'integer', 'null' => false,

'key' => 'primary'),
'glossaryterm_id' => array('type' => 'integer',
'null' => false),
'category_id' => array('type' => 'string',
'null' => false)
);
}

This class definition represents three tables: glossaryterms , categories, and a lookup table to facilitate the relationship between the two tables. Each variable in the class represents a table, and the array keys inside of the variable represent the fields in the table. As you can see, the first two tables match up with our earlier architecture description.

Creating the database schema

On the command line, assuming you haven't moved to any other folders, type the following command:

Console/cake schema create glossary

You should then see the following responses. When prompted, type y once to drop the tables, and again to create them.

Welcome to CakePHP v2.2.3 Console
---------------------------------------------------------------
App : app
Path: /path/to/app
---------------------------------------------------------------
Cake Schema Shell
---------------------------------------------------------------
The following table(s) will be dropped.
glossaryterms
categories
glossaryterms_categories
Are you sure you want to drop the table(s)? (y/n)
[n] > y
Dropping table(s).
glossaryterms updated.
categories updated.

glossaryterms_categories updated.
The following table(s) will be created.
glossaryterms
categories
glossaryterms_categories
Are you sure you want to create the table(s)? (y/n)
[y] > y
Creating table(s).
glossaryterms updated.
categories updated.
glossaryterms_categories updated.
End create.

If you look at your database now, you will notice that the three tables have been created. We can also make modifications to the glossary.php file and run the cake schema command again to update it.

If you want to try something a little more daring, you can use the migrations plugin found at https://github.com/CakeDC/migrations. This plugin allows you to save "snapshots" of your schema to be recalled later, and also allows you to write custom scripts to migrate "up" to a certain snapshot version, or migrate "down" in the event of an emergency or a mistake.

Summary

In this article we saw the use of the schema tool and also its database.

Resources for Article:


Further resources on this subject:


Instant CakePHP Starter [Instant] Learn everything you need to develop a feature-rich CakePHP app, from installation to deployment with this book and ebook
Published: April 2013
eBook Price: $12.99
See more
Select your format and quantity:

About the Author :


Mark Robert Henderson

Mark Robert Henderson has been understanding, developing, modifying, and deploying PHP applications since 2006, and while he has been through several love affairs with different technologies over the years, he tries to remain as unopinionated as possible about specific technologies, mostly to avoid arguments with other developers.

However, sometimes a technology comes along that just makes an engineer smile at its simplicity and elegance. CakePHP is one of those, and Mark is happy to share some of the cool things that he's learned in the course of creating applications with it.

Books From Packt


CakePHP 1.3 Application Development Cookbook
CakePHP 1.3 Application Development Cookbook

CakePHP Application Development
CakePHP Application Development

PHP Application Development with NetBeans: Beginner's Guide
PHP Application Development with NetBeans: Beginner's Guide

CouchDB and PHP Web Development Beginner's Guide
CouchDB and PHP Web Development Beginner's Guide

PHP Ajax Cookbook
PHP Ajax Cookbook

PHP jQuery Cookbook
PHP jQuery Cookbook

Expert PHP 5 Tools
Expert PHP 5 Tools

Object-Oriented Programming with PHP5
Object-Oriented Programming with PHP5


Your rating: None Average: 1.7 (3 votes)

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
i
K
D
h
3
i
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software