Rails-like ActiveRecord for PHP 5.3

Saturday, September 13, 2008

I’ve worked on a small project that makes Rails-like AciveRecord available for PHP 5.3. It’s unimaginatively enough called active-record-php. I think an approach of this nature would make a really viable competitor to the likes of Propel and Doctrine and I’ll tell you why!

It works exactly like Rails’ ActiveRecord. There are two features in PHP 5 which makes this possible (the magic function __call - equivalent to method_missing in Ruby) and something called late-static binding which is only available in PHP 5.3. __call allows dynamic method invocation and late-static binding (along with a few tricks) simulates class and instance methods in Ruby. Let’s see some code!


user.php:
<?php
 
require_once 'base.php';
 
class User extends ActiveRecord::Base {
  protected static $class = __CLASS__;
  
  public static $associations = array(
    'has_many' => array(
      'photos'
    )
  );
};

?>

photo.php:
<?php
 
require_once 'base.php';
 
class Photo extends ActiveRecord::Base {
  protected static $class = __CLASS__;
  
  
};
?>

That’s a basic has_many relationship. Include the base.php class (which lives in its own namespace), and that’s it. No more configuring schema.xml, regenerating boilerplate get/setter code, etc. It’s all in there.

How do you use it? Again, intuitive:


<?php
 
require_once 'user.php';
require_once 'photo.php';
 
ActiveRecord::Base::establish_connection(array(
  'host' => 'localhost',
  'database' => 'test',
  'username' => 'root',
  'password' => 'root'
));
 
$users = User::find_all();
$user = $users[0];
$user->user_name_set("Love");
$user->save();
print_r($user->photos());
print_r($user);

That’s all there to it. And less than 150 lines of code too!

Of course, this is more like a POC. The only stuff implemented are attribute getters and setters, a simple save(), a has_many relationship etc. No validation, the rest of the relationships, eager loading, et. al. But it’s easily doable. If somebody wants access to the github project do let me know! It does require PHP 5.3 though (which is currently alpha 2) but do give it a try: a siege benchmark showed 5.3 to be around 30% faster than 5.2.6 for a simple caffeine (note: that’s a link to my fork: the original is here) application, so 5.3 would be good!

Comments are welcome!

Comments (6) | Permalink

View Vishnu Gopal's profile on LinkedIn

Twitter

Thursday, September 25, 2008

Automatic Rails at Slicehost

Automatic Rails at Slicehost

The Ruby on Rails blog for Rails application performance and monitoring. Rails tutorials, troubleshooting tips, error fixes, podcasts, examples and other Rails development resources.

Rating: ★ ★ ★ ★ ★

(0) #

Wednesday, September 24, 2008

Dan Manges’s Blog - Rails: Performance Tuning Workflow

Dan Manges's Blog - Rails: Performance Tuning Workflow

Pretty amazing workflow

Rating: ★ ★ ★ ★ ★

Tags: , ,

memagent - Google Code

memagent - Google Code

Memagent is pretty cool! I was thinking about writing something like this but more like a memcached-http connector thingy.

Rating: ★ ★ ★ ★ ★

Tags: , , , ,

(0) #

Sunday, September 21, 2008

Amazon.com: King of Bollywood: Shah Rukh Khan and the Seductive World of Indian Cinema: Anupama Chopra: Books

Amazon.com: King of Bollywood: Shah Rukh Khan and the Seductive World of Indian Cinema: Anupama Chopra: Books

Amazon.com: King of Bollywood: Shah Rukh Khan and the Seductive World of Indian Cinema: Anupama Chopra: Books

Rating: ★ ★ ★ ★ ★

Tags: ,

(0) #

Friday, September 19, 2008

The Pragmatic Bookshelf | Cocoa Programming

The Pragmatic Bookshelf | Cocoa Programming

Cocoa Programming: A Quick-Start Guide for Developers, , by Daniel H Steinberg, 978-1-9343563-0-2, Mar 2009

Rating: ★ ★ ★ ★ ★

Tags: , ,

Panda - Open source video platform

Panda - Open source video platform

Panda could make web server streaming easy. No support for 3gp hmm.

Rating: ★ ★ ★ ★ ★

Tags: , , , ,

(0) #

Wednesday, September 17, 2008

Cmockery

Cmockery

Unit testing for C! :)

Rating: ★ ★ ★ ★ ★

(0) #

Friday, September 12, 2008

Simple Easy Parallel Processing in PHP | PHP Everywhere

Simple Easy Parallel Processing in PHP | PHP Everywhere

Useful for FA

Rating: ★ ★ ★ ★ ★

(0) #

Thursday, September 11, 2008

Anybody who’s having problems with a Nokia 6300, try a firmware upgrade!

(0) #

y

y

30128

Rating: ★ ★ ★ ★ ★

(0) #

Wednesday, September 10, 2008

caffeine-php - Google Code

caffeine-php - Google Code

Rating: ★ ★ ★ ★ ★

Tags: ,

Bort - A Base Rails Application | Ruby on Rails

Bort - A Base Rails Application | Ruby on Rails

An Introduction When I set up a new rails app, I generally do the same things every time. Set up a config file, install RESTful authentication, add password

Rating: ★ ★ ★ ★ ★

Digg - Throw out the Kindle, Plastic Logic has something better

Digg - Throw out the Kindle, Plastic Logic has something better

Get ready to finally chuck all those newspapers and books that you’ve been carrying because Plastic Logic has introduced a full-page sized ebook reader that doesn’t suck.

Rating: ★ ★ ★ ★ ★

PURE Unobtrusive Rendering Engine for HTML

PURE Unobtrusive Rendering Engine for HTML

PURE is an Open Source and Unobtrusive JavaScript Template Engine for HTML

Rating: ★ ★ ★ ★ ★

Web Application on Erlang: So Far, So Good, … | BeeBuzz

Web Application on Erlang: So Far, So Good, … | BeeBuzz

We are building our web application BeeLit (SaaS) on Erlang and so far, the adventure runs smoothly.

Rating: ★ ★ ★ ★ ★

Tags: , ,

(0) #

Tuesday, September 9, 2008

Ubuntu and Debian AMIs for Amazon EC2 - Alestic.com

Ubuntu and Debian AMIs for Amazon EC2 - Alestic.com

Rating: ★ ★ ★ ★ ★

Tags: , , , ,

Cocoamug Software / CosmoPod

Cocoamug Software / CosmoPod

Rating: ★ ★ ★ ★ ★

Tags: , , , , ,

caffeine-php - Google Code

caffeine-php - Google Code

Rating: ★ ★ ★ ★ ★

Tags: ,

(0) #