Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228
Object Relational Mapping with PHP and MySQL

Comments Blog About Development Research Sites

Object Relational Mapping with PHP and MySQL

Jan 20, 2008
There are many nifty things one can do by really adhering to the OO principles. Unfortunately, for those like me who have started out using a procedural style they are not readily accesable.

Just the mere useage of classes does not merrit all; in fact, it can be a disadventage when used improperly as opposed to procedural coding. I like to think there are two kind's of programmers: those who write object oriented code, and those who use classes.

Not so long ago I definately belonged in the later catagory, so as anyone set on selfimprovement would do I figured I'd get some good training to fix that. So, I decided to write a ORM system - not an easy task, and definately impossible to do without a really good use of objects.

Of course it is all still in the preliminary stages, yet selecting and updating rows already works quite well:
Code (php) (nieuw venster):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
global $context;
// Get the 'menu' table from our database:
$menu         = $context -> db -> menu;

// Get the record with ID 5:
$menu -> id   = 5;

// Retrieve the record and output it's name:
echo $menu -> name;  // Echo's: 'Register'

// Get the record with ID 2:

$menu -> id = 2;

// Retrieve the record and store it in an object 'name':
$name =  $menu -> name;

// Set a new value for this record's 'name' column:
$name -> name = 'Logout';

// Update the record in the database:
$name -> update();

// Retrieve the name again - this time the updated
// value is returned.  

echo $name -> name;  // Echo's: 'Logout'


The most difficult part about this for me was being able to 'call back' - a result object needed to know what his parent was so he would be able to execute updates on records with the same limitations. In addition, if the parent's parameters were changed the resultobject should perform updates with these new parameters. This meant using a lot of references, never an easy task, and in addition preventing recursiveness that would consume server resources at ten times the really required rate.

When that's all said and done the system also needs to function without a minimum of effort on the programmers part, and to that end I opted to use __call methods to allow virtually any default PHP function to work on the results:

Code (php) (nieuw venster):
1
2
3
4
5
6
7
8
9
10
11
12
// Retrieve the menu table:
$menu         = $context -> db -> menu -> get();

// For each item, echo the name:
while ($name = $menu -> each())
  echo
$name -> name;

// Want the first item? No problem!    
echo $menu -> first() -> name;

// Or the ID of the next one? Easy!
echo $menu -> next() -> id;


I've been implementing a lot of these small options over the past two days and the result is starting to 'feel' quite usefull, yet it still has not seen any reall production work - which as always tends to be the 'real' testing ground. Currently there are 9 different classes required to do this, forming about 350 lines of code, and undoubtedly there are many more to come, so one could argue that just writing the original project could have been halfway done by now as well. Still, as I said originally, it's one hell of an exercise into OOP :)

FragFrog out!

Feb 14, 2008 SchizoDuckie

check out mijn implementatietje in 500 regels (ORM + AR ) via sourceforge: http://sourceforge.net/projects/pork-dbobject/

Feb 14, 2008 Matthijs

I've actually seen it already (I think you linked to it in your sig on GoT?) ;)

Especially impressive how you use primary keys by find relationships. Definately worth looking into!

New comment

Your name:
Comment: