Page 1 of 1

Objects and Databases

Posted: Wed Feb 07, 2007 5:00 pm
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

Posted: Wed Feb 07, 2007 5:12 pm
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.

Posted: Wed Feb 07, 2007 5:38 pm
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.

Posted: Wed Feb 07, 2007 6:39 pm
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.

Posted: Thu Feb 08, 2007 6:49 am
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

Posted: Thu Feb 08, 2007 11:45 am
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);
                }

Posted: Thu Feb 08, 2007 12:01 pm
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 ;-)