Friday, November 30, 2007

P.O.R.K (Modified to work with PHP 4)

For those who don't know what is PORK, it's an Object-Relation mapper for PHP 5 that attempts to be easy, fast, lightweight, uses optimized database queries and provides an easy Find() function very loosely based on rails'.

In short, the O/R mapper handles all your basic Select/Insert/Update/Delete/Join statements for you so you won't have to write the same SQL over and over. You just create PHP objects that are linked to a row in the database.

The above description and original work is from mr. Jelle Ursem (see http://www.schizofreend.nl/ for more info). I came accross his work when I was about to create one Library to do exactly like PORK does. The only problem I had to work with it was that my servers runned PHP4, so I modified the code to fit my requirements.

So, again, I posted PORK for those that still work with PHP4 and have a need for an O/R mapper like this one.

Jell Ursem have plenty of more detailed info related to the functionality of this object (for PHP 5) and also an PORK.generator (A PHP5 based database analyzer that automatically sees the different types of relations in your (MySQL) database and creates linked dbObjects for it). So check out its site for more detailed info.

Differences from PHP5 version and this PHP4 version
Object Creation
Check the following object declaration example, it is an O/R mapper that will work with table tbl_note, we call it Note object (differences with the object in PHP5 are clear if you check Jell examples on http://www.schizofreend.nl/Pork.dbObject/Examples/)

class Note extends dbObject
{
function Order($ID=false) {
$this->__setupDatabase('tbl_note', // database table
array( 'n_ID' => 'ID', // database field => object property
'o_ID' => 'orderID',
'n_note' => 'note',
'n_date' => 'date'),
'n_ID', // primary table key
$ID); // value of primary key to init object

// Add one-to-one relationship
$this->addRelation('Order'); // Order is another object and addRelation function will connect both of them
}
// This function is a *MUST* dbObject requires it
function toString()
{
return 'Note';
}
}

I have a included a mandatory function toString to the objects, so we can overcome the lack of a function such as get_class($object); in PHP4 PORK.dbObject.

Accessing Object Properties
We all know that PHP4 objects do not have the _set and _get capabilities of PHP5, therefore, in order to access object properites we will have to do it in this way:

$note = new $note();
$note->__set('note','my note'); // PHP5 $note->note = 'my note';
echo $note->__get('note'); // PHP5 $note->note;
$note->Save(); // saving to database

I have compiled a set of files on a zip file, if you are good code reader you will find out how to use very easily. There a file with an example of its use, but I did not include any SQL file to create the tables nor a proper test application. The test.php file included explain most of the steps and Jell on its site will pinpoint you to the right direction if you are missing something.




No comments:


The content of this blog is published under a Creative Commons License | RSS Feed
Powered By Blogger