CodeIgniter and Datamapper

In PHP, my choice is CodeIgniter framework v.2, a lightweight, zero-configuration, open source OOP MVC framework (Object-oriented programming, model-view-controller framework) which allows you to efficiently split your presentation from logic. And more important, it is really zero-configuration, you can just upload it and start working, everything just works! You can then set/alter configuration later as you work, and their documentation is simply awesome.

Though I know (My)SQL to a certain degree, and sooner or later I’ll make a query that works (more or less efficiently), working with database takes a lot time off my productivity. Therefore I chose Datamapper ORM (Object-relational mapping). Why this and not CodeIgniter’s native models? Well, in college I was simply taught to always make an object one-to-one copy of database table row and this is exactly what Datamapper ORM gives me. I mean, look at this:

//fetch artist by name
$artist = new Artist();
$artist->where('title', 'Primus')->get();

//fetch its albums
$artist->albums->get();
foreach ($artist->albums as $album)
{
 echo "Album: {$album->title} ($album->year)\n";
}

It so easy and natural to work with this. You just need to define a PHP class for each model and set couple of settings inside of it, define relationships (if any) and that’s it! Everything works out of the box. You can of course make custom methods in your model class which can perform certain operations on itself as well as on other (related) models.

I am also exploring Laravel lately, but given the fact that British Columbia Institute of Technology has acquired CodeIgniter and is heavily working on v.3, Laravel might just wait a little bit.

Leave a Reply

Your email address will not be published. Required fields are marked *