= PHP Active Record Class = {{{ #!NewsFlash = This page needs an update! = Beware that the code listed here has numerous bugs and is only intended for research purposes. Please [wiki:contactMe contact me] if you are interested in a newer, more polished version. }}} I had seen and heard a lot about [http://en.wikipedia.org/wiki/Active_record_pattern Active Record] and wanted to have some fun with it. As I'm mostly a PHP guy, I decided to roll out my own class. This is what I came up with. It never really made it to ''production'' so it's probably a bit rough, but I've learned a lot about the internals of AR and frankly, had a lot of fun programming this. It has support for renderer class extensions (ie: to generate an xml output), in model validation functions, etc. I've used the occasion to get accustomed with [http://xdebug.org/ xDebug], the PHP remote debugger. You can view a live example [http://code.mlalonde.net/dbtmp/v2.php here] (reload to see the effect). '''Note that this is test code and the errors are expected for debugging'''. See attached file for the source. == Usage Example == {{{ #!php _init(__CLASS__, $renderer); } protected function onCreate() { $tableName = $this->myName; $tableSQL = <<fetchRow(HP('limit', 1)) > 0) return true; else return false; } /** * Custom, auto-called, field validation function **/ protected function validate_login($data) { if (rand(0, 1) == 0) return true; else return false; } } // END table model Pages $myTest = new activeRecordTest(new xmlRenderer); $myTest->login = 'User'; $myTest->password = 'password'; // Use of a custom model method echo 'User jdoe ' . (($iExists = $myTest->loginExists('jdoe')) ? 'exists' : 'doesn\'t exist') . "\n"; if ($myTest->fetchAll() == 0 || !$iExists) { $myTest->email = 'jdoe@example.org'; $myTest->password = sha1('jdoe'); $myTest->login = 'jdoe'; if ($myTest->saveData()) { echo 'Saved data 1' . "\n"; } else { echo 'Failed to save 1' . "\n"; } } $myTest->fetchRows(HP('fieldName', '*')); ?>

tableRenderer generated XML


render(true)); ?>
}}}