Scaffolding with the command-line tool

Instant CakePHP Starter


April 2013

$10.00

Learn everything you need to develop a feature-rich CakePHP app, from installation to deployment

(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:


Books to Consider

comments powered by Disqus