With MySQL setup and the basic Scorpio databases installed and configured, we can create a new database:
tutorials
and then create a new table "guestbook". We will use the "tutorials" database in all of these tutorial articles. The SQL code for these two is below:
CREATE DATABASE `tutorials` ; CREATE TABLE `tutorials`.`guestbook` ( `id` INT( 10 ) NOT NULL AUTO_INCREMENT , `name` VARCHAR( 100 ) NOT NULL , `emailAddress` VARCHAR( 100 ) NOT NULL , `ipAddress` VARCHAR( 20 ) NOT NULL , `referrer` VARCHAR( 255 ) NOT NULL , `comment` TEXT NOT NULL , `createDate` DATETIME NOT NULL , `updateDate` DATETIME NOT NULL , `updatedFor` ENUM( 'Language', 'Off topic', 'In-appropriate Content', 'Other Reason' ) NOT NULL DEFAULT 'Language', PRIMARY KEY ( `id` ) ) ENGINE = MYISAM
Be sure to update the main config.xml file in /libraries with an entry under the database section for "tutorials".
Next, we need a terminal window (command line, this should work for Windows too). Go to your project location and then into the tools directory. Inside there is a script called "scorpio.php". You can run this by calling:
php scorpio.php
This will run the script and show you the list of options available. Right now, we are going to list the databases:
php scorpio.php list database all
Our "tutorials" database should be listed. Next we can list all the tables within the tutorials database using:
php scorpio.php list database tutorials
Now we are going to generate our guestbook DAO object.
php scorpio.php new dao tutorials.guestbook --classname=guestbook
Hit enter, and it should complete without trouble. You may need to create the target folder first. This example attempts to create a class named "guestbook.class.php" within a sub-folder called guestbook in the main classes folder within the framework root in the current project folder.
You can check that this was built correctly by browsing to the folder and opening up the file. You can see what generator has done - it has built the basic structure for a DAO object including all the properties and methods to save, load and delete records as well as listing multiple records. All the properties are protected and are accessed via get/set methods. This is working from the default template for class files. If this is not to your liking you can create any number of custom templates. This will be covered in a separate tutorial looking at all of the generator features.
You will need to create an autoload file in the classes/autoload folder. This is a very simple file that just returns an array of the classname with the path to the file relative to the classes folder:
return array(
'guestbook' => 'guestbook/guestbook.class.php',
);
We can additionally create a test case skeleton for our class by using the scorpio CLI tool to build one for us.
To do this we run the following command:
php scorpio.php new test guestbook --package=tutorials
This will create a test case within the /data/tests folder (unless configured differently).
To run the test case you need to return to the command line, and then run:
php testSuite.php test package tutorials guestbook
The test will run and should return mostly skipped entries - it is up to you to complete the test case.
Posted by: Dave Redfern (Writer), in Tutorials on 12 Apr 2008 @ 13:51
Tags: dao, generator, guestbook, mvcgenerator, scorpio, tutorial,
Contents:
Related Articles
This
work is licenced under a
Creative Commons Licence.