Two Weeks of Suck

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.

Comments

But isn't that ineffecient, looping through the data twice?

    by Lewis on Mar 25, 2007 @ 4:20am

Where do you get two loops from?

Hoi Peter!

That's nothin' new, isn't it? You already use such a records system in OneLobby, dont' you? Kinda.

Keep it up! =o)

    by Thasmo on Mar 29, 2007 @ 5:21am

Thasmo,

The AR scheme that I made is about 90% the same as the one Geoffrey made for FA; however, it is all php5. The main reason why I made it is because I wanted to get back into php5 because I haven't really used it since k4bb v1 and also to learn how the system works inside-out.

Add a Comment