Objects and Databases

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
amk221
Forum Newbie
Posts: 6
Joined: Wed Feb 07, 2007 4:35 pm

Objects and Databases

Post by amk221 »

Usually when I code a small web app, I do everything procedurally, making use of functions here and there. I try to code as efficiently as possible.
I'm hoping to do my next project using objects...

Imagine I have a database table called Tests, and I wanted to echo everything in it.

Originally I would have done this:

Code: Select all

$query = mysql_query("SELECT * FROM Tests");
while ($row = mysql_fetch_object($query)) {
	echo $row->name, ', ',  $row->address, "\n<br />";
}
This is my OO interpretation of it...

Code: Select all

<?php
class Test {
	private $id, $name, $address;

	public function get_id()		{return $this->id;}
	public function get_name()	{return $this->name;}
	public function get_address()	{return $this->address;}

	public function set_name($name)		{$this->name = $name;}
	public function set_address($address)	{$this->address = $address;}
	public function load_test($test_id) {
		$query = mysql_query("SELECT * FROM Tests WHERE id = '$test_id'");
		while ($row = mysql_fetch_object($query)) {
			$this->id = $row->id;
			$this->name = $row->name;
			$this->address = $row->address;
		}
	}
	public function save_test()	{}
	public function delete_test()	{}
}

$my_test = new Test();
$my_test->load_test(1);
echo $my_test->get_name(), ', ', $my_test->get_address(), "\n<br />";
?>
Is that the proper way of doing things?
Surely it can't be, because I can only access ONE Test at a time

All I want to do is display everything in the Test table

It seems daft retrieving the data from the table, and then making a New Test Object for each row!

Hope you can correct my vague understanding of oo
Last edited by amk221 on Thu Feb 08, 2007 6:53 am, edited 4 times in total.
wei
Forum Contributor
Posts: 140
Joined: Wed Jul 12, 2006 12:18 am

Post by wei »

I would try to code to be as maintainable as possible (which is not always easy).

What you have there is the begining of an Active Record,

http://www.martinfowler.com/eaaCatalog/ ... ecord.html

and seems to work fine for individual tables/views, but beware of complications if you start to use joins in your queries.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

You should

Code: Select all

tags rather than

Code: Select all

for your php, many people would appreciate it if you edit your post accordingly.

You could write an object called test that pulls all of the records then supply it with methods for getting certains sets of the data or single rows.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Since you are using PHP5 you could use __get, __set and __call to create a simple ActiveRecord class and then just give it the table and key names. Then you would not have to create a new class each time (or inherit if you need to).

Also, not sure why you use a while() in your load_test() method ... and error check might be better.
(#10850)
amk221
Forum Newbie
Posts: 6
Joined: Wed Feb 07, 2007 4:35 pm

Post by amk221 »

Thanks all.
arborint or anyone, could you tell me more about ActiveRecord, I've googled it and my understanding is it's a design pattern? But I can't find much info about it and how it relates to (my)sql.

also, what is the alternative to my while statement
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

amk221 wrote:Thanks all.
arborint or anyone, could you tell me more about ActiveRecord, I've googled it and my understanding is it's a design pattern? But I can't find much info about it and how it relates to (my)sql.
ActiveRecord encapsulates a single database row as a ValueObject. I has the ability to load, save and delete that row. It can also can be extended to contain business logic so it can be used as a Model.
amk221 wrote:also, what is the alternative to my while statement
You only load one row so you don't need a while. It should probably be something like:

Code: Select all

if (! mysql_errno($query)) {
                     $row = mysql_fetch_object($query);
                } else {
                     $row = null;
                     $this->error = mysql_error($query);
                }
(#10850)
amk221
Forum Newbie
Posts: 6
Joined: Wed Feb 07, 2007 4:35 pm

Post by amk221 »

Ok cool, so now I have my Test class and Test_DAO class.

When I make a new Test_DAO i can call load_test($test_id), which I've named more appropriately get_test_by_id($id), and it returns a Test object. That more like?

Re the while loop - good point ;-)
Post Reply