Object-Oriented Database Design Pt. 2
55Weaving OO design into your DB
The HELIX PHP Framework was designed to enforce powerful, robust data manipulation by integrating object oriented methods into table design. The previous article describes how the table design integrates object inheritance. This allows for child and parent objects to construct using the same unique identifier. This article will show you how the constructors work.
The example given involved 3 tables: cor_entity, cor_person, and cor_user.
cor_entity.id == cor_person.entity_id == cor_user.person_entity_id
We can now construct any level of parent-child as follows:
<?php $entity = new entity(1); $person = new person(1); $user = new user(1); ?>
The following is a stripped version of the entity constructor:
public function __construct($__id=null)
{
$this->_id = 0;
$this->_description = "";
$this->_entity_type_id = 0;
$this->_mdate = null;
$this->_cdate = null;
$this->_deleted = false;
if (is_null($__id))
{
return;
}
$db = entity::db();
$q = " SELECT cor_entity.id,cor_entity.description, cor_entity.entity_type_id, cor_entity.mdate, cor_entity.cdate,cor_entity.deleted ";
$q .= " FROM cor_entity ";
if (!is_null($__id))
{
$q .= " WHERE id = '$__id' ";
}
$db->query($q);
if ($db->next_record() && $db->num_rows() == 1)
{
$this->_id = $db->f("id");
$this->_description = $db->f("description");
$this->_entity_type_id = $db->f("entity_type_id");
$this->_mdate = $db->f("mdate");
$this->_cdate = $db->f("cdate");
$this->_deleted = $db->f("deleted");
}
}
The entity object constructs off of the `id` column in the `cor_entity` table. The person object inherits the entity object and constructs off of the `id` column of `cor_entity` as well:
public function __construct($__entity_id=null)
{
$this->__id = 0;
$this->_entity_id = 0;
$this->_first_name = "";
$this->_middle_name = "";
$this->_last_name = "";
$this->_suffix = "";
$this->_nickname = "";
$this->_initials = "";
$this->_person_type_id = 0;
$this->_mdate = null;
$this->_cdate = null;
$this->_deleted = false;
$this->_id = 0;
$this->_description = "";
$this->_entity_type_id = 0;
if (is_null($__entity_id))
{
return;
}
$db = person::db();
$q = " SELECT cor_entity.id,cor_entity.description, cor_entity.entity_type_id, cor_person._id,cor_person.entity_id, cor_person.first_name, cor_person.middle_name, cor_person.last_name, cor_person.suffix, cor_person.nickname, cor_person.initials, cor_person.person_type_id, cor_person.mdate, cor_person.cdate, cor_person.deleted ";
$q .= " FROM cor_person ";
$q .= " INNER JOIN cor_entity ON cor_person.entity_id=cor_entity.id ";
$q .= is_null($__entity_id) ? "" : " AND cor_entity.id='$__entity_id' ";
$db->query($q);
if ($db->next_record() && $db->num_rows() == 1)
{
$this->_id = $db->f("id");
$this->_description = $db->f("description");
$this->_entity_type_id = $db->f("entity_type_id");
$this->__id = $db->f("_id");
$this->_entity_id = $db->f("entity_id");
$this->_first_name = $db->f("first_name");
$this->_middle_name = $db->f("middle_name");
$this->_last_name = $db->f("last_name");
$this->_suffix = $db->f("suffix");
$this->_nickname = $db->f("nickname");
$this->_initials = $db->f("initials");
$this->_person_type_id = $db->f("person_type_id");
$this->_mdate = $db->f("mdate");
$this->_cdate = $db->f("cdate");
$this->_deleted = $db->f("deleted");
}
}
You can see that the `cor_person` constructor joins the `cor_entity` table and includes the parent table columns as part of itself.
The user object constructs off of the unique identifier of its parent column, which is `person_entity_id`. This is equivalent to the `id` of the `cor_entity` table. This shows how all children of an object construct using the top most `id`:
public function __construct($__person_entity_id=null, $__username=null)
{
$this->__id = 0;
$this->_person_entity_id = 0;
$this->_username = $__username;
$this->_password = "";
$this->_authmethod_id = 0;
$this->_user_type_id = 0;
$this->_userstatus_id = 0;
$this->_inactive = false;
$this->_mdate = null;
$this->_cdate = null;
$this->_deleted = false;
$this->_entity_id = 0;
$this->_first_name = "";
$this->_middle_name = "";
$this->_last_name = "";
$this->_suffix = "";
$this->_nickname = "";
$this->_initials = "";
$this->_person_type_id = 0;
$this->_id = 0;
$this->_description = "";
$this->_entity_type_id = 0;
if (is_null($__person_entity_id) && is_null($__username))
{
return;
}
$db = user::db();
$q = " SELECT cor_entity.id,cor_entity.description,cor_entity.entity_type_id, cor_person.entity_id, cor_person.first_name, cor_person.middle_name, cor_person.last_name, cor_person.suffix, cor_person.nickname, cor_person.initials, cor_person.person_type_id, cor_user._id, cor_user.person_entity_id, cor_user.username, cor_user.password, cor_user.authmethod_id, cor_user.user_type_id, cor_user.userstatus_id, cor_user.inactive, cor_user.mdate, cor_user.cdate, cor_user.deleted ";
$q .= " FROM cor_user ";
$q .= " INNER JOIN cor_person ON cor_user.person_entity_id=cor_person.entity_id ";
$q .= is_null($__person_entity_id) ? "" : " AND cor_person.entity_id='$__person_entity_id' ";
$q .= is_null($__username) ? "" : " AND cor_user.username='$__username' ";
$q .= " INNER JOIN cor_entity ON cor_person.entity_id=cor_entity.id ";
$db->query($q);
if ($db->next_record() && $db->num_rows() == 1)
{
$this->_id = $db->f("id");
$this->_description = $db->f("description");
$this->_entity_type_id = $db->f("entity_type_id");
$this->_entity_id = $db->f("entity_id");
$this->_first_name = $db->f("first_name");
$this->_middle_name = $db->f("middle_name");
$this->_last_name = $db->f("last_name");
$this->_suffix = $db->f("suffix");
$this->_nickname = $db->f("nickname");
$this->_initials = $db->f("initials");
$this->_person_type_id = $db->f("person_type_id");
$this->__id = $db->f("_id");
$this->_person_entity_id = $db->f("person_entity_id");
$this->_username = $db->f("username");
$this->_password = $db->f("password");
$this->_authmethod_id = $db->f("authmethod_id");
$this->_user_type_id = $db->f("user_type_id");
$this->_userstatus_id = $db->f("userstatus_id");
$this->_inactive = $db->f("inactive");
$this->_mdate = $db->f("mdate");
$this->_cdate = $db->f("cdate");
$this->_deleted = $db->f("deleted");
}
}
You can see that the `cor_user` constructor joins the `cor_person` table and `cor_entity` table. The properties of the user includes the properties of person and entity.
This method allows the HELIX PHP Framework to use focused tables that become increasingly powerful using intelligent object inheritance.
PrintShare it! — Rate it: up down flag this hub






