PDA

View Full Version : PHP Object Design


pkp
08-13-2003, 10:19 PM
Hey there, I've just begun developing objects in PHP, I'm familiar with object oriented design in general but have not attempted to create such a beast before relating to databases.

Basically, I'm curious if anyone has any tips or tutorials you could point me to regarding this:

I have a few tables, two of which are indexes for the primary table, such as:


Table 1 (Primary)
Name CityID ObjectTypeID
Josh 1 1
Mathew 2 1
Rick 1 2

Table 2 (Cities)
ID CityName
1 San Francisco, CA
2 Minneapolis, MN

Table 3 (Objects)
ID ObjectName
1 Rubber Ball
2 Harmonica


So... I want to create an object that I can use to modify and deal with Primary Table entries. The problem I run into is that I need to be able to query the city and object names, but I don't know if I should do it with seperate queries or what.

Anybody else write objects relating to pulling or related directly to a record (records) in a database?

Hope that all makes sense...

Thanks!

Jason
08-13-2003, 10:23 PM
use loops after you do a query of the db, which includes changing it, to go to the other tables and see if anything needs to be changed there with more querys.


Jason

pkp
08-14-2003, 07:26 AM
That would make sense, I could do that, I was hoping to find that there was some trick or technique that people use to prevent doing multiple queries to deal with this problem...

firepages
08-14-2003, 06:01 PM
you can use the 1 query ?

SELECT Primary.name , Cities.CityName , Objects.ObjectName FROM Primary
LEFT JOIN Cities ON Primary.CityID = Cities.ID
LEFT JOIN Objects ON Primary.ObjectTypeID = Objects.ID
WHERE Primary.Name=$id

or something like that anyway .. if thats what you meant ?

a primary objective for many OOP'ers is to abstract the SQL competely out of your primary objects into its own class , that can mean anything from a simple abstraction ...

bad simplistic example...

class DB{
function DB(){
mysql_connect(...blah
}

function query($SQL){
$rets = mysql_query($SQL);
return $rets;
}

function get_array($res){
while($yaks = mysql_fetch_array($res,MYSQL_ASSOC){
...... etc


$db= & new DB();
$db->query("SELECT ... etc

right up to a DAO where you have a class full of functions specific to your needs where for instance you may have a method that specifically calls the join above.

$dao->get_user_objects($username);

that removes all the SQL from your main logic which is considered by some to be a good thing , by others to be essential and by some a waste of time ;)

whats best is comparable to the length of a peice of string.

pkp
08-15-2003, 03:33 PM
I guess my post was more related to the most efficient way of updating multiple tables while having an 'object' which correlates itself with data from multiple tables.

I think I'll write a basic object capable of knowing wether its value matches the database, then something to talk with these objects and automatically build appropriate update/insert/select queries to pull and post information to the database.

Anyone know if you can define a class variable through code within the class?