Our Axis Entity Object class is quite powerful and useful for lightweight ORM. But lets go ahead and create a help class that will alleviate a lot of business and view coding by automating the create and update forms and processing for Axis Entity Objects.
First lets look at how the code will operate in the business side of things. Here is how we generate the creation form for our example Axis Entity Object class “Fruit”:
<form action="index.php" method="post">
<?php AxisObjectHelper::DrawFormCreate('Fruit'); ?>
<input type="submit" />
</form>
What you can see here is that our helper class will contain a static method for creating an update form for our class “Fruit”. Lets take a look at that code.
public static function DrawFormCreate($class)
{
// get our fields from our mapped database table
$fields = AxisObjectHelper::GetFields($class);
// loop through the returned fields and generate input fields matching the name of the columns.
foreach($fields as $field_object)
{
if($field_object->Key != 'PRI')
{
echo '<div class="object_field" id="create_'.$class.'_field_'.$field_object->Field.'">'.self::PrettifyName($field_object->Field).'</div>';
echo '<div class="object_input" id="create_'.$class.'_input_'.$field_object->Field.'">'.self::DrawInput($field_object).'</div>';
}
}
}
Generating the create form is simple as pie once we have the corresponding columns of the class.
Here is how we process the create form:
AxisObjectHelper::ProcessFormCreate('Fruit');
So simple! Again we have a single line of PHP for processing the user’s input and creating the object. Lets take a closer look at that static method.
public static function ProcessFormCreate($class)
{
// get our table columns
$fields = AxisObjectHelper::GetFields($class);
// create a new instance of our Axis Entity Object
$create = new $class();
// loop through the fields and create them
foreach($fields as $field_object)
{
if($field_object->Key != 'PRI')
{
$field_name = $field_object->Field;
$create->$field_name = $_POST[$field_name];
}
}
return $create->Create();
}
Using the Axis Entity Object class, we leverage it’s ORM to create the object very simply.
Updating objects is just as simple requiring only two lines of PHP to generate the form and to process it. Here is the example helper class in its entirety.
class AxisObjectHelper
{
public static function OpenMySQL($host, $db, $user, $pass)
{
mysql_connect($host, $user, $pass);
mysql_select_db($db);
}
public static function DrawFormCreate($class)
{
// get our fields from our mapped database table
$fields = AxisObjectHelper::GetFields($class);
// loop through the returned fields and generate input fields matching the name of the columns.
foreach($fields as $field_object)
{
if($field_object->Key != 'PRI')
{
echo '<div class="object_field" id="create_'.$class.'_field_'.$field_object->Field.'">'.self::PrettifyName($field_object->Field).'</div>';
echo '<div class="object_input" id="create_'.$class.'_input_'.$field_object->Field.'">'.self::DrawInput($field_object).'</div>';
}
}
}
public static function ProcessFormCreate($class)
{
// get our table columns
$fields = AxisObjectHelper::GetFields($class);
// create a new instance of our Axis Entity Object
$create = new $class();
// loop through the fields and create them
foreach($fields as $field_object)
{
if($field_object->Key != 'PRI')
{
$field_name = $field_object->Field;
$create->$field_name = $_POST[$field_name];
}
}
return $create->Create();
}
public static function DrawFormUpdate($object)
{
$class = get_class($object);
$primary_key_column = strtolower($class).'_id';
$fields = AxisObjectHelper::GetFields($class);
foreach($fields as $field_object)
{
if($field_object->Key != 'PRI')
{
$field_name = $field_object->Field;
$object_field_value = $object->$field_name;
echo '<div class="object_field" id="update_'.$class.'_field_'.$field_object->Field.'_'.$object->$primary_key_column.'">'.self::PrettifyName($field_object->Field).'</div>';
echo '<div class="object_input" id="update_'.$class.'_input_'.$field_object->Field.'_'.$object->$primary_key_column.'">'.self::DrawInput($field_object, $object_field_value).'</div>';
}
}
}
public static function ProcessFormUpdate($class, $object_id)
{
$fields = AxisObjectHelper::GetFields($class);
$update_object = call_user_func(array($class, 'Factory'), $object_id, $class);
foreach($fields as $field_object)
{
if($field_object->Key != 'PRI')
{
$field_name = $field_object->Field;
$update_object->$field_name = $_POST[$field_name];
}
}
return $update_object->Update();
}
public static function DrawInput($field_object, $value = '')
{
// cases for input
if(strpos($field_object->Type, 'tinyint') !== false && strpos($field_object->Type, '(1)') !== false)
{
if($value != '')
{
if((bool)$value)
{
return '<input type="checkbox" class="input_boolean" name="'.$field_object->Field.'" value="1" checked="checked" />';
}
}
return '<input type="checkbox" class="input_boolean" name="'.$field_object->Field.'" value="1" />';
}
if (
strpos($field_object->Type, 'int') !== false ||
strpos($field_object->Type, 'varchar') !== false ||
strpos($field_object->Type, 'float') !== false ||
strpos($field_object->Type, 'decimal') !== false ||
strpos($field_object->Type, 'double') !== false ||
strpos($field_object->Type, 'real') !== false ||
strpos($field_object->Type, 'date') !== false ||
strpos($field_object->Type, 'time') !== false
)
{
return '<input type="text" class="input_varchar" name="'.$field_object->Field.'" value="'.htmlentities($value).'" />';
}
if(strpos($field_object->Type, 'text') !== false)
{
return '<textarea class="input_text" name="'.$field_object->Field.'">'.$value.'</textarea>';
}
}
public static function PrettifyName($name)
{
return ucwords(str_replace('_' , ' ', $name));
}
// MySQL Specific
public static function GetFields($class)
{
$primary_key_column = strtolower($class).'_id';
$table = DB_T_PREPEND.strtolower($class);
$cmd = "SHOW COLUMNS FROM ".$table."";
$result = mysql_query($cmd);
while($row = mysql_fetch_object($result))
{
$cols[] = $row;
}
return $cols;;
}
}
This helper class is simply used as an example for automating CRUD using Axis Entity Objects. We can easily modify the methods apply business rules for validating the object by providing that method in our “Fruit” class and calling its Validate() method. We can also adjust the methods for other databases.
That is our limitation with this example helper. Each database has different ways of parsing the columns of a table. This example was specific to MySQL.


