The past two weeks have been really hectic! I've have to do two papers, an exam, a quiz, and I'm now working on a business case competition. Beside that, I wrote a simple active records scheme in PHP5 that I was planning on writing about at some point. Here's a quick demo of how it works if you don't know what I'm talking about...
<?php
error_reporting (E_STRICT);
define ("PROJECT_DIR", dirname(__FILE__));
// database
require PROJECT_DIR .'/database.php';
require PROJECT_DIR .'/drivers/mysql.php';
// general functions
require PROJECT_DIR .'/functions.php';
// records
require PROJECT_DIR .'/record.php';
require PROJECT_DIR .'/recordset.php';
// finder
require PROJECT_DIR .'/finder.php';
// models
require PROJECT_DIR .'/model.php';
require PROJECT_DIR .'/table.php';
/*
* test!
*/
// connect to the database
$dba = new DBA_MySQL;
$dba->connect('localhost', 'db', 'user', 'pass');
// bring in the model and pass the database connection
// to it
$model = new Model($dba);
// get a model definition and finder for the table 'wt_course_names'
$finder = $model->getFinder('course_names');
// find all the rows in this table
$results = $finder->findAll();
$i = 1;
while($results->next())
{
// get the current record!
$course = $results->current();
// change the name of this course
$course->name = 'test course '. $i;
// save it!
$course->save();
$i++;
}
// free the database result
$results->free();
?>
Notice how this not only abstracts the database, but also the tables themselves! If you were ever wondering what and how models worked in MVC frameworks, the upcoming article about this code will help you out.
- Development
- PHP by Peter Goodman on Mar 24, 2007 @ 7:33pm

