Scaffolding
Right now, Tor doesn't do a whole lot. It lets people register, log in,
and see who else is registered. Now what it needs is the ability for
users to enter some products into the catalog or view some products
from other users. A good way to get a jump-start on this is to use
scaffolding.
Scaffolding is a concept that comes from Ruby on Rails (see
Resources). It's an excellent way to get
some structure built quickly to prototype the application, without
writing a bunch of throwaway code. But scaffolding, as the name
implies, is something that should be used to help build an
application, not something to build an application around. Once you
start wishing the scaffolding acted differently, it's time to pull it
down.
Setting up the product
tables
Scaffolding works by examining the database tables and creating the
basic types of elements normally used with a table: lists,
add/delete/edit buttons, the stuff normally called Create, Read,
Update, Delete (CRUD). To start, you need some tables to hold product
information and dealer information.
Listing 4. Creating tables to hold product information
CREATE TABLE 'products' (
'id' INT( 10 ) NOT NULL AUTO_INCREMENT ,
'title' VARCHAR( 255 ) NOT NULL ,
'dealer_id' INT( 10 ) NOT NULL ,
'description' blob NOT NULL ,
PRIMARY KEY ('id')
) TYPE = MYISAM ;
CREATE TABLE 'dealers' (
'id' INT( 10 ) NOT NULL AUTO_INCREMENT ,
'title' VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ('id')
) TYPE = MYISAM ;
|
Additionally, it will be helpful for this demonstration to insert some
data into the dealers table.
INSERT INTO dealers (title)
VALUES ('Vogon Construction Corp'), ('Bistromathics, Inc')
|
An important note about scaffolding: Remember that note from setting up
the database about foreign keys following the format
singular_id like
user_id or
winner_id? In CakePHP, scaffold will expect
that any field ending in _id is a foreign
key to a table with the name of whatever precedes the _id
— for example, scaffolding will expect that
dealer_id is a foreign key to the table
dealers.
Setting up the product
model
The products functionality represents a whole new set of models, views,
and controllers. You'll need to create them as you did in Part 1.
Create your product model in app/models/product.php.
Listing 5. Creating a product model
<?php
class Product extends AppModel
{
var $name = 'Product';
var $belongsTo = array ('Dealer' => array(
'className' => 'Dealer',
'foreignKey'=>'dealer_id')
);
}
?>
|
You'll notice the $belongsTo variable. This
is what's known as a model association.
Model associations
Model associations tell a model that it relates in some way to
another model. Setting up proper associations between your models will
allow you to deal with entities and their associated models as a
whole, rather than individually. In CakePHP, there are four types of
model associations:
-
hasOne
- The
hasOne association tells the model
that each entity in the model has one corresponding entity in
another model. An example of this would be a user entity's
corresponding profile entity (assuming a user is only permitted
one profile).
-
hasMany
- The
hasMany association tells the model
that each entity in the model has several corresponding entities
in another model. An example of this would be a category model
having many things that belong to the category (posts, products,
etc.). In the case of Tor, a dealer entity has many products.
-
belongsTo
- This tells a model that each entity in the model points to an
entity in another model. This is the opposite of
hasOne, so an example would be a
profile entity pointing back to one corresponding user
entity.
-
hasAndBelongsToMany
- This association indicates that an entity has many corresponding
entities in another model and also points back to many
corresponding entities in another model. An example of this might
be a recipe. Many people might like the recipe, and the recipe
would have several ingredients.
The belongsTo variable in this case
indicates that each product in the products table "belongs to" a
particular dealer.
Creating the dealer
model
As the association implies, a dealer model is also required. The dealer
model will get used later in Tor to build out the functionality to
define dealerships. Whereas the product model had an association of
belongsTo pointing at dealer, the dealer
model has an association to product of
hasMany.
Listing 6. The dealer model has an association to
product of hasMany
<?php
class Dealer extends AppModel
{
var $name = 'Dealer';
var $hasMany = array ('Product' => array(
'className' => 'Product',
'foreignKey'=>'dealer_id')
);
}
?>
|
You can skip adding data validation for now, but as the application
evolves, you may get ideas for different types of validation to add.
Creating the products
controller
You've built and associated the models for product and dealer. Now Tor
knows how the date is interrelated. Next, make your controller in app/controllers/products_controller.php
— but this time, add the class variable
$scaffold.
Listing 7. Adding a class variable to your controller
<?php
class ProductsController extends AppController
{
var $scaffold;
}
?>
|
Save the controller, then visit http://localhost/products (yes, without
creating any views or a Dealer controller). You should see something
like Figure 1.
Figure 1. Empty product
list
It's really that simple. Just like that, you have an interface into
your products table that lets you add, edit, delete, list, slice, and
julienne your products.
Try adding a product. You should be prompted to enter a title and a
description, as well as select a dealer. Did that list
of dealers look familiar? It should have — you inserted them into the
dealer table just after you created it. Scaffolding recognized the
table associations as you defined them and auto-generated that
drop-down dealer list for you.
Now go back and look at the amount of code you wrote to get all of this
functionality. How much easier could it get?
|