Helix Installation   Getting Started   Reference Documentation   Global Functions   Quick Start
Getting StartedHelpful ResourcesAsk questions on the Help ForumRead the User GuideBrowse the Reference DocumentationSubscribe to the Helix Mailing ListLearning HelixAdvanced Topics

Database Customization

Helix easily allows you to alter and extend the pre-packaged database. You can add properties to an existing object, new objects, or new relationships.   

  1. The first step is writing the SQL file. Follow the database design rules for writing your SQL file, and place your SQL file in the following directory:
    Helix/
        sites/
            mysite/
                etc/
  2. Run your SQL on your site database.
  3. Run the generator to create all of the class files
  4. Place the generated files in your site's lib directory:
    Helix/
        sites/
            mysite/
                lib/

Why do I have to follow the helix data design rules?

The class design is dependent on the data structure. The power of helix begins at data design. The table names, column names, foreign keys, ... allow the helix code-generator to build a robust set of classes that are extremely powerful.

Example 1: Adding a custom property to an existing object

If you want to add a property to an exisiting object, the easiest way is to extend the object. Let's say we want to add a column 'height' to the user object. We will create a myuser object. The SQL for this object will look like this:


SET NAMES utf8;
SET SQL_MODE='';
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `mysite_myuser`;
CREATE TABLE `mysite_myuser` (
  `_id` INT(11) NOT NULL AUTO_INCREMENT,
  `user_person_entity_id` INT(11) NOT NULL DEFAULT 0,
  `height` TEXT,
  `mdate` DATETIME DEFAULT NULL,
  `cdate` DATETIME DEFAULT NULL,
  `deleted` TINYINT(1) DEFAULT 0,
  PRIMARY KEY (`person_entity_id`),
  UNIQUE KEY `_id` (`_id`),
  CONSTRAINT `FK_mysite_myuser_1` FOREIGN KEY (`user_person_entity_id`) REFERENCES `cor_user` (`person_entity_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

Run the generator and create the class files. Your object is ready to go:

<?php
    
// contstruct an extended user, this user will have all of the properties of the normal user, plus the `height`
    
$user = new myuser($user_id);
    
    
$user->height "6'2\"";
    
    
//save the user
    
$user->commit();
?>

Example 2: Creating a new object

Let's say you need a new object widget. The first thing you will do is write the SQL file:


SET NAMES utf8;
SET SQL_MODE='';
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `mysite_widget`;
CREATE TABLE `mysite_widget` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `property` TEXT,
  `mdate` DATETIME DEFAULT NULL,
  `cdate` DATETIME DEFAULT NULL,
  `deleted` TINYINT(1) DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `_id` (`_id`),
  CONSTRAINT `FK_mysite_myuser_1` FOREIGN KEY (`user_person_entity_id`) REFERENCES `cor_user` (`person_entity_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

Run the generator and create the class files. Your object is ready to go:

<?php
    
// contstruct an extended user, this user will have all of the properties of the normal user, plus the `height`
    
$widget = new mywidget($widget_id);
    
    
$widget->property "6'2\"";
    
    
//save the widget
    
$widget->commit();
?>

Example 3: Creating a new relationship

Let's say you want to link widget to user. The first thing you will do is write the SQL file:


SET NAMES utf8;
SET SQL_MODE='';
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `mysite_user_widget`;
CREATE TABLE `mysite_user_widget` (
  `id` INT(11) NOT NULL auto_increment,
  `user_person_entity_id` INT(11) NOT NULL DEFAULT 0,
  `widget_id` INT(11) NOT NULL DEFAULT 0,
  `user_widget_type_id` INT(11) NOT NULL DEFAULT 0,
  `primary` TINYINT(1) NOT NULL DEFAULT 0,
  `mdate` DATETIME DEFAULT NULL,
  `cdate` DATETIME DEFAULT NULL,
  `deleted` TINYINT(1) DEFAULT 0,
  PRIMARY KEY (`user_person_entity_id`,`widget_id`,`user_widget_type_id`),
  UNIQUE KEY `id` (`id`),
  CONSTRAINT `FK_mysite_user_widget_1` FOREIGN KEY (`user_person_entity_id`) REFERENCES `cor_user` (`person_entity_id`),
  CONSTRAINT `FK_mysite_user_widget_2` FOREIGN KEY (`widget_id`) REFERENCES `mysite_widget` (`id`),
  CONSTRAINT `FK_mysite_user_widget_3` FOREIGN KEY (`user_widget_type_id`) REFERENCES `mysite_user_widget_type` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `mysite_user_widget_type`;
CREATE TABLE `mysite_user_widget_type` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` TEXT,
  `description` TEXT,
  `mdate` DATETIME DEFAULT NULL,
  `cdate` DATETIME DEFAULT NULL,
  `deleted` TINYINT(1) DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`(32))
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `mysite_user_widget_type`(`id`,`name`,`description`,`deleted`) VALUES (0,'default','Default',0);

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

Run the generator and create the class files. Your object is ready to go:

<?php
    
// contstruct a user
    
$user = new user($user_id);
    
$widget = new mywidget($widget_id);
    
    
//you can link them in any of the following ways:
    
$user->widget_id $widget->id;
    
//OR
    
$user->widget $widget;
    
//OR
    
$widget->user_id $user->id;
    
//OR
    
$widget->user $user;
    
    
//save the relationship
    
$user->commit();
?>
Get Helix PHP Framework at SourceForge.net. Fast, secure and Free Open Source software downloads