#native_company# #native_desc#

Value Objects and Data Access Objects with PHP 4.2.x

By Geno Timlin
on June 1, 2004


My real job involves working with J2EE applications at this time. It pays the bills.
I’ve worked with ASP in the past and have recently (2003) gotten more into working with PHP.
Most of my sidework these days is data driven PHP/MySQL web sites. It’s unfortunate that
sidework doesn’t pay the bills, because I have found PHP to be a very powerful programming platform.
My attempt here is to apply some principles and concepts that I have learned in my work with
J2EE into my work with PHP.

Value Objects

The purpose of a Value Object is to represent a business entity. In it’s simplest form, a
VO is a simple data mapping class that mirrors the entity as it exists in the database. A
more complex VO may incorporate other functionality in its methods. For example, a VO may
have a method that transforms it’s data into an XML node or a VO may be able to read it’s
data from an HTML form. The one thing the VO doesn’t have to worry about is the database.
The main purpose of the VO is to store data as the data comes in and out of the database.

Data Access Objects

Data Access Objects act as the middle man between the database and the Value Objects.
The base DAO class knows specifically how to talk to the database. The base DAO knows which
database is being used, what kind of database it is and whether it’s supposed to be working with
mysql_*, mssql_* or pg_* methods. The base DAO is essentially the Data layer or Model part of the MVC
The DAO classes that extend the base DAO class can use the base DAO’s methods to select, insert,
update and delete data. The extended DAO classes don’t have to worry about whether they are talking
to a MySQL database or a PostGreSQL database. If you switch from MySQL to PostGreSQL, you can change
the base DAO class and the extended DAO’s will not be affected. The extended DAO’s are the business
logic layer or the Controller part of the MVC.
DAO’s and VO’s Working Together
The extended DAO classes utilize the VO’s to retrieve data and update data. If you want a single
row of data from the database, based on the record’s primary key value, the extended DAO will
have a method that returns a single VO. The extended DAO will have a method that returns an array of
VO’s if you want more than one row of data. The extended DAO handles inserts, updates and deletes
with methods that take a single VO as an argument.
The Application
In the example code, I’m presenting some of the functionality of a simple auto sales web site.
We’ll look at the vehicles that are stored in the database. The database is a MySQL database and
the vehicles table is created with the following properties:

CREATE TABLE `vehicles` (
`year` VARCHAR( 4 ) NOT NULL ,
`make` VARCHAR( 25 ) NOT NULL ,
`model` VARCHAR( 25 ) NOT NULL ,
`color` VARCHAR( 25 ) NOT NULL ,
PRIMARY KEY ( `vehicleid` ) 


Comment and Contribute

Your comment has been submitted and is pending approval.


Geno Timlin