Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Value Objects and Data Access Objects with PHP 4.2.x
Introduction
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` (
`vehicleid` INT NOT NULL AUTO_INCREMENT,
`year` VARCHAR( 4 ) NOT NULL ,
`make` VARCHAR( 25 ) NOT NULL ,
`model` VARCHAR( 25 ) NOT NULL ,
`color` VARCHAR( 25 ) NOT NULL ,
`price` DOUBLE NOT NULL ,
PRIMARY KEY ( `vehicleid` ) 
);
[ Next Page ]


Comments:
RE: Code FixJayee08/09/08 17:01
Security "ouch!" at the form part...Dave10/04/07 09:20
Code FixJason10/30/05 15:36
php scriptolipa03/30/05 05:04
one detailIsrael Dominguez01/28/05 15:11
PHP Best PracticesJeroen10/16/04 04:32
RE: php codePests06/22/04 14:01
Small problemLonnie Olson06/02/04 19:03
Interesting but where are all the files?Henrik06/02/04 12:47
php codeSimon06/02/04 02:44
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.