PHPBuilder - Value Objects and Data Access Objects with PHP 4.2.x Page 4



RSS Twitter
Articles Databases

Value Objects and Data Access Objects with PHP 4.2.x - Page 4

by: Geno Timlin
|
June 1, 2004

The parent constructor knows the default database connection parameters, but you could specify the values in the constructor of the extended class too if you were working with multiple databases.
The VehicleDAO object keeps it's own simple SQL queries. This makes the data access methods easier to implement. The SELECT queries are made to allow you to append your own WHERE clause parameters, ORDER BY clauses and LIMIT clauses (for mySQL). The data manipulation SQL statements are formatted so that it takes little extra code inside the data access methods to set the values to be inserted or updated.

<?php
var $SQL_SELECT "SELECT * FROM `vehicles` ";
var 
$SQL_COUNT "SELECT count(*) AS cnt FROM `vehicles` ";
// insert with no value for an auto_increment field.
var $SQL_INSERT "INSERT INTO `vehicles` (year,make,model,color,price) VALUES ('%A','%B','%C','%D',%E)";
var 
$SQL_UPDATE "UPDATE `vehicles` SET ";
var 
$SQL_DELETE "DELETE FROM `vehicles` WHERE vehicleid=%A";
?>
The VehicleDAO object contains a number of data access methods:
function findByPK($vehicleid) -
Returns a single VehicleVO object based on the $vehicle id or null if no record is found.
function findBySQL($sql, $orderby = "") -
Returns an ordered array of VehicleVO's based on a complete SQL query.
For example:

<?php

$dao 
= new VehicleDAO();
$volist $dao->findBySQL("select * from vehicles where make='Ford' ""year, model");
foreach(
$volist as $vo
{
    echo(
"$vo->year $vo->model $vo->color<br />");
}
?>
The above would produce a list of Ford vehicles ordered by year and model.

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Geno Timlin

Comment:



Comment:

(Maximum characters: 1200). You have characters left.