Tuesday, February 28, 2006

 

Php, Ruby (on Rails), and Java... and AJAX

Ok, let me start by saying that I developed (and taught) enterprise Java for a few years, so I am familiar with the various tools and techniques in that space, including J2EE and EJB (v.2) in particular.

I'm sure lots of people have commented on this, but EJB is really heavy weight and not suitable for most situations. When I was using EJB however, I was impressed by CMP - basically a way to create an automatic Object-Relational mapping so you can use objects in your application instead of programming SQL statments directly.

The problem with CMP, however, is first you have to use the entire EJB framework, which means you have to learn the EJB way of doing things. Basically lots of XML configuration files and code generation. I personally dislike both.

Lately, scripting languages have taken the web programming world "by storm". Starting with PHP, Python, and now Ruby.

Ruby on Rails, a framework developed using the Ruby scripting language, is an attempt to make database access transparent to the application programmers by performing automatic O-R mapping, in the same spirit as EJB (but much much simpler). I think that is very exciting as all the tutorials advertised that you can write a complete database-backed application in 15 minutes.

The excitment started to fade a little as I started following the tutorials. I realized that in order to use that framework, I need to learn to use the various tools that comes with Rails, including code generation and configuration files. Have I mentioned that I dislike either of these.

So now I'm back to square one in the O-R mapping area. I then came across this php article that describes the dynamic features of PHP classes. That inspire me to think that I can use the techniques described in the article to create an abstract class that performs dynamic O-R mapping (without code generation and configuration files!).

My goal is essentially this: If I have a table org that contains information of different organizations, I should be able to create the following class in PHP...

class Org extends DBObject {
}

Since Org inherits from the abstract DBObject (which performs the O-R mapping), I can now operates on Org as follows:

// Create an organization
org = new Org;
org->set_name('ABC Charity');
org->create();

// Retrieves the and update
org = org->retrieve("name = 'ABC Charity'");
org->set_name('My Charity');
org->update();

// Deletes a charity
org->delete();

// Might as well add a toXML method in the abstract class to enable an AJAX interface
echo org->toXML();

I spent a few days working on the code, and guess what, it really works! The DBObject class uses the ADOdb library and requires PHP5. It is still a work in progress but here it is.

Pre-condition: Make sure you have a database and at least one table defined. You also need to install the ADOdb library and include them inside DBObject.php

DBObject.php

This is the class that performs the magic O-R mapping. Simply extend this class and name your subclass the same name as your database table. Be sure to edit the connection line with your own host, user, password and database (Lines 9 and 10).

It even has a simple toXML() method to output XML for AJAX processing by the browser.

One more thing: This class also performs foriegn key look up similar to Rails and EJB. Check out the code in the __get() and __call() magic methods.

DefaultController.php

Following the Model View Controller architecture, this DefaultController accepts HTTP requests and pass the parameters to the appropriate class for processing.

controller.php

Edit this file to include your own class declarations.

The main entry page for entering commands. It creates a DefaultController and pass parameters into it. You can test it by using issuing the following URL in your browser:

http://yourdomain/controller?class=org&method=retrieve&param=id>0

This will retrieve all records of id > 0 for the table org (assuming you have a table named "org").

index.php

A very simple AJAX enabled client to issue commands to controller.php (so you don't have to write long URLs).

I have only tested these files against a mysql installation. I would appreciate it if you can help me test and improve my code.

Jason

Saturday, February 25, 2006

 

Hello World

I have been wanting to blog for the longest time, so now here I am.

Who am I and what is this all about? I teach computer science at a 2-year college and I have been a professional software developer for the last 13 years. I have strong opinions on IT and Computer Science and would like to share them with the world (as well as to have a place for myself to remember them :) )

Enjoy your stay!

This page is powered by Blogger. Isn't yours?