BusinessBase
,and make it the base class of all business classes. BusinessBase performs the application
utility tasks (permissions, transaction logging, etc.). It also handles every SQL statement.
None of the business classes ever connect to a database directly. Instead, they use a data
structure to exchange information with BusinessBase.
<?php
class Hippo extends BusinessBase
{
var $id = null;
var $name = null;
//Constructor
function Hippo()
{
$this->table_name = 'hippos';
$this->addField('id', new DataField(DataField::TYPE_NUMERIC(), true) );
$this->addField('name', new DataField(DataField::TYPE_STRING(), false) );
}
//Load record from DB
function load($id)
{
parent::load($id);
$this->id = $this->getDBValue('id');
$this->name = $this->getDBValue('name');
}
//Save record to DB
function save() {
$this->setDBValue('id', $this->id );
$this->setDBValue('name', $this->name );
parent::save();
//Get id number supplied by INSERT
$this->id = $this->getDBValue('id');
}
}
?>
First, it sets the name of the RDB table used to store hippo data (line 6). Then it specifies
each field in the table, supplying the name of the field, and a DataField object (lines 7 and 8).
A DataField object contains the data type of a field, and a flag showing whether or not it is
the primary key.
Hippo::load()
passes a record id toBusinessBase::load()
(line 12), then extracts the values it needs(id and name in this case, in lines 13 and 14).
Hippo::save()
stores the values it wants to save (lines 18 and 19), and calls
BusinessBase::save()
(line 20). If
Hippo::save()
is creating a new record,BusinessBase::save()
supplies its id number, whichHippo::save()
then retrieves (line 22).