Home > CFWheels, ColdFusion > CFWheels — Creating a Custom Model

CFWheels — Creating a Custom Model

August 25th, 2009

Continuing the series on using Head First Rails to learn CFWheels, lets move on to Chapter 2 — Beyond Scaffolding. We are going to create the MeBay application!

Starting on Page 45 in Head First Rails …

Whether we use scaffolding or not, we still have to create the database. In Rails, you do this with the script/generate model command and then running rake. But, in CFWheels, it’s easiest to just use SQL and create the database directly. Here is some mySQL code to create the needed ads table.

[sql]

CREATE TABLE `ads` (
`id` INTEGER PRIMARY KEY NOT NULL auto_increment,
`name` varchar(255),
`description` text,
`price` decimal,
`seller_id` integer,
`email` varchar(255),
`img_url` varchar(255),
`created_at` datetime,
`updated_at` datetime
);
[/sql]

Later on, we’ll want to populate the table with some sample data, so let me go ahead and show the full SQL that creates the table and adds the test data:

[sql]
DROP TABLE IF EXISTS `ads` ;
CREATE TABLE `ads` (
`id` INTEGER PRIMARY KEY NOT NULL auto_increment,
`name` varchar(255),
`description` text,
`price` decimal,
`seller_id` integer,
`email` varchar(255),
`img_url` varchar(255),
`created_at` datetime,
`updated_at` datetime
);
INSERT INTO `ads` VALUES(1,’Typewriter’,'Old manual typewriter. Many years useful service. Works best with a bottle next to it.’,71.95,54,’dhammett@email.com’,'http://homepage.mac.com/david_griffiths/typewriter.png’,”,”);
INSERT INTO `ads` VALUES(2,’Football’,'Some strings frayed.’,74.02,45,’marty@googlemail.com’,'http://www.freefever.com/freeclipart/clipart/football2.gif’,”,”);
INSERT INTO `ads` VALUES(3,’Moosehead’,'Slightly moth-eaten. One of the antlers is broken and there’’s a strange buzzing sound behind the eyes…’,2978.25,56,’kathy@hotmail.com’,'http://saloon.javaranch.com/ubb/moosefly.gif’,”,”);
INSERT INTO `ads` VALUES(4,’Desk’,'Milk desk – go…’,4800,123,’andy@allmail….’,'http://picasaweb.goog…’,”,”);
INSERT INTO `ads` VALUES(5,’Door curtain’,'Beaded door cu…’,11,773,’dawn@hotmail….’,'http://www.freewebsit…’,”,”);
INSERT INTO `ads` VALUES(6,’Apple Newton’,'Still works! M…’,25,2,’ahertz@differ…’,'http://www.differnet….’,”,”);
INSERT INTO `ads` VALUES(7,’Sinclair C5′,’Battery flat t…’,372.06,346,’clive@sinclai…’,'http://www.ebay.com/w…’,”,”);
INSERT INTO `ads` VALUES(8,’Edsel’,'Never used aut…’,355,755,’bing@badabing…’,'http://pages.google.c…’,”,”);
INSERT INTO `ads` VALUES(37,’Diamond’,'203.4 carats, grade D pear cut diamond. Low price for quick sale. Owner leaving the country.’,1000000,5234,’pink@panther.com’,'http://’,”,”);
[/sql]

It’s easy enough to manually create the Model, but for convenience, let’s use the scaffolding plugin anyway.

Click the Scaffold link at the bottom of any CFWHeels page –  In the debug section, next to Plugins. Then, on the scaffold page, enter ad as the Object name, select Model from the Type drop down, and click Generate. Remember, the table is named ads (plural with an “s”), but the the model is named ad (singular).

Now, open /models/Ad.cfc. You’ll see something that looks like:

[cf]
<cfcomponent extends="Model">

<!— All initialization related tasks are done in the "init" function which is run the first time the model is requested. —>
<cffunction name="init">
</cffunction>

</cfcomponent>
[/cf]

That’s all it takes to create a model! Your model must extend Model.cfc — That’s where all the CFWheels goodness happens. Other than that, all you need is an init function. Later on, you can use the init function to add extra capabilities, but for now, all that’s needed is a blank init function!

Clarke Bishop CFWheels, ColdFusion

  1. No comments yet.
  1. No trackbacks yet.