Posts tagged OO
P.O.R.K. Object Relation Mapper
0
For those who don’t know what is P.O.R.K., 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/CREATE 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.
Jell Ursem have plenty of more detailed info related to the functionality of this object (for PHP 5) and also an P.O.R.K. 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.
How it works
Imagine you need to manipulate and query a database table called ‘tbl_eyecolor’, we then create a class like this:
class EyeColor extends dbObject // it extends the dbObject class from the PORK
{
function __construct($ID=false)
{
// I can include custom methods within the constructor
// here we check whether the table is created and if not, we create it
// with the dbConnection object from PORK
if(!Library::checkDBTable('tbl_eyecolor'))
{
dbConnection::getInstance()->query("
CREATE TABLE divinesecrets.tbl_eyecolor (
idEyeColor int(11) NOT NULL auto_increment,
strName varchar(125) default NULL,
PRIMARY KEY (idEyeColor)
) ENGINE=MyISAM DEFAULT CHARSET=utf8");
}
// setting up the database
// and correspondent fields
$this->__setupDatabase('tbl_eyecolor', // db table
array('idEyeColor' => 'ID', // db field => object property
'strName' => 'strName'),
'idEyeColor', // primary db key
$ID); // primary key value
}
}
How to use the class? Easy
// adding new $ec = new EyeColor(); $ec->strName = 'Brown'; $ec->Save(); // updating $ec = new EyeColor(1); // 1 = the ID of the row we want to update $ec->strName = 'Green'; $ec->Save(); // deleting $ec->DeleteYourSelf();
And that’s it!
I cannot stop talking about the wonders of P.O.R.K. I develop three times faster any web application (web panel). I truly believe that you should check it out, and thank me after
PHP4 Version?
For those who still using PHP4 and wish to use it, do not hesitate to contact me, I wrote a PHP4 version of such wonder set of objects to suit my past needs.
Tweet this!