Class Generator v1.0 Documentation

Class Generator is a simple Code Generator in building Database Scalfolding for building application. It generates accessor methods (GET and SET) for all the database fields defined in the class, CRUD (Create, Retrieve, Update, Delete) methods for performing basic database operation, and List method.

These essential methods are all needed in building application without actually coding everything from scratch. The code generated can be modified anytime to fully customize the whole application.

The Class Generator is bundled with a simple UI that would help or guide you in generating your own class.

Setting Up

Class Generator Class Package: Directory Structure

/ClassGenerator (the directory of the Class Generator)
/core_classes (Classes needed to perform class generation and other essential functions)
DBGEN_Generator.php (Generator Class)
core.dbfunctions.php (MySQL Database Wrapper)
/images (Images for the Generator UI)
/objects (Default temporary Storage for the Generated Class, This directory can be customized)
/style (CSS for Generator UI)
configuration.php (Database Configuration script)
index.setup.php (Generator UI)

configuration.php

This is the default configuration script that the system use in determining the classes needed to be generated. There are simple steps in setting up the Generator

<?php
// include files
include 'core_classes/core.dbfunctions.php';
include 'core_classes/DGEN_Generator.php';

// Database Configuration
$dbConn = new DbConnection();
$dbConn->useManualDefinition("hostname", "databasename", "username", "password");
$dbConn->doConnection();	
?>
To set up the database connection, you have to modify the configuration.php and change it to your own database connection parameters. After modifying the configuration, Run the index.setup.php

index.setup.php

The index.setup.php generally provides you with all the essential UI in generating the class. If you have setup the database configuration properly it should display all the needed database tables and its corresponding fields. All you have to do is to follow all the instructions provided in the index.setup.php

The Generated Class

All the generated class follows simple format in all of its method.

Accessor Methods:
CRUD METHODS

Example

If you have this table (ITEM) with database fields id, itemname, description
ITEM: {id, itemname, description}

Field Name Type  
id (PRIMARY) INT autonumber
itemname VARCHAR(150)  
itemdescription TEXT  

Generator UI setup:

Generated Class: Item

<?php
class Item {
    var id;
    var itemname;
    var itemdescription;
	
	// ACCESSOR METHODS: GET
    function get_id() { ... }
    function get_itemname() { ... }
    function get_itemdescription() { ... }

    // ACCESSOR METHOD: SET
    function set_id($id) { ... }
    function set_itemname($itemname) { ... }
    function set_itemdescription($itemdescription) { ... }
	
    // CRUD METHOD
    function createnew_item($_id, $itemname, $itemdescription) { ... }
    function get_item($_id) { ... }
    function update_item($_id, $itemstobeupdated = array()) { ... }
    function delete_item($_id) { ... }
    function list_item($conditions) { ... }
}	
?>
This is the output of the generated class. The class provides a simple access interface between you application and the database itself, so you dont have to write SQL queries just to get repeated information, saving you amount of time.

Using the Class: Item

The class uses the database object as a MySQL wrapper (core.dbfunctions.php) defined in the globals as $GLOBALS['dbConn']
Setting up the Database wrapper object is simple all you have to do it to define it as a global variable
<?php
    $dbConn = new DbConnection(); 
    $dbConn->useManualDefinition(*my connection parameters*);
    $dbConn->doConnection();
	
    // Declare the dbConn object to the globals
    $GLOBALS['dbConn'] = $dbConn;
?>
Normally what i do it to put it inside a configuration file or script the include it to all my pages. I reuse the connection class to all my projects using the included configuration file. Once you have set this up, you can already use the class to perform its operation.

Creating New Entry: Item

<?php
    // include the file of the generated class
    include_once 'path/to/generated/objects/item.class.php';
    
    // create a new instance
    $item = new Item();

    // creating a new record
    $item->createnew_item(0,'Rolex Wrist Watch', 'Rolex Wrist Watch detailed Description');
?>
All you have to do is to fill all the parameters provided by the method in inserting, in this case, the method requires, the item id (auto number), the item name, and the description.
Note: If you are using an autonumber fields, use 0 (zero) as a value for those fields.
Make sure you validate all your input before inserting into the database.

Updating Existing Entry: Item

<?php
    // include the file of the generated class
    include_once 'path/to/generated/objects/item.class.php';
    
    // item id to be updated
    $itemIdToBeUpdated = 1;
	
    // create a new instance
    $item = new Item();

    // fields to be updated
    // fields are added into an associative array with its corresponding new value
    $pprop = array("itemname" => "Rolex Diamond Wrist Watch",
                   "itemdescription" => "Diamond watch rolex... updated description.");
    
	// updating the record
    $item->update_item($itemIdToBeUpdated, $pprop);
?>

Deleting an Existing Entry: Item

<?php
    // include the file of the generated class
    include_once 'path/to/generated/objects/item.class.php';
    
    // item id to be updated
    $itemIdToBeDeleted = 1;
	
    // create a new instance
    $item = new Item();
  
	// delete the record
    $item->delete_item($itemIdToBeDeleted);
?>

Retriving an Existing Entry: Item

<?php
    // include the file of the generated class
    include_once 'path/to/generated/objects/item.class.php';
    
    // item id to be updated
    $itemIdToBeRetrived = 1;
	
    // create a new instance
    $item = new Item();
  
	// item to be retrived
    $itemNoOne = $item->get_item($itemIdToBeRetrived);
	
    // after passing it to a value you can get all the values of the object by using the accessor methods
$itemNoOne->get_id(); $itemNoOne->get_itemname(); $itemNoOne->get_itemdescription(); ?>

Listing an Existing Entry: Item

<?php
    // include the file of the generated class
    include_once 'path/to/generated/objects/item.class.php';
    
    // create a new instance
    $item = new Item();
  
    // items to be retrived with its matching SQL condition
    // NO WHERE needed, groupings and other conditions can be added in this part
    $items = $item->list_item("itemname LIKE '%Rolex%'");

    foreach($items as item) {	
        // after passing it to a value you can get all the values of the object by using the accessor methods
$item->get_id(); $item->get_itemname(); $item->get_itemdescription(); } ?>