#native_company# #native_desc#
#native_cta#

A Practical Approach to Object-Relational Mapping in PHP Page 9

By PHP Builder Staff
on March 8, 2004

<?php

class DataField 

{

    function 
TYPE_STRING() 

    { 

        return 
1

    }

    function 
TYPE_NUMERIC() 

    { 

        return 
2

    }

    var 
$db_type null;

    var 
$is_primary_key null;

    var 
$value null;

    function 
DataField($col_type$is_pk

    {

        
$this->db_type $col_type;

        
$this->is_primary_key $is_pk;

    }

}

?>



Lines 2 and 3 essentially define two constants, though they are really static methods and are called as such
(see lines 7 and 8 in the Hippo code above, and line 7 in ArmedHippo). Declaring them this way subjects
them to the same scope and inheritance rules as other methods.
BusinessBase::addField() (called by Hippo and ArmedHippo) adds a field to
$records:

<?php

function addField$db_col_name$field 

{    

    
$this->record[$db_col_name] = $field;

}

?>



BusinessBase::load() looks like this:

<?php

function load($id

{

    
$this->find_id_field();

    
$conn mysql_connect('localhost''hippo_user''hippo_user');

    
mysql_select_db('hippos');

    
$query 'select * from '.$this->table_name.' where '.$this->_id_field." = $id";

    
$fetched_record mysql_fetch_assocmysql_query($query) );

    foreach (
$this->record as $col_name=>$field)

    
$this->record[$col_name]->value $fetched_record[$col_name];

    
application_utility_tasks();

}

?>



BusinessBase::load() calls find_id_field()
in line 3 to locate the primary key field, and store its name in $this->_id_field for later use.
The method then opens the database (line 4), builds a SELECT query (line 7), and executes it
(line 9). Lines 11-13 move the data into $record, where it can be retrieved with a statement
like this from Hippo:
$this->id = $this->getDBValue(‘id’);

1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|