I am trying out CodeIgniter and know of Active Records the very first time. In the beginning I ignored it as being something for those who don't really understand how to write SQL. I understand since my analysis was problematic and Active Records are pretty prominent, particularly in Rails.

But what purpose do Active Records hold? Could it be to abstract from different RDBMS individualities. If that's the case I figured that is not that what SQL is supposed to do. In addition what's best practice, must i be utilising these?

Thanks ahead of time

Active Record is really a design pattern for data access...

Right now you will find two primary design designs I appear to encounter regarding data access: ActiveRecord and also the Repository Pattern

Active Record

Your objects contain techniques for persisting their condition to some DB (or any other persistence mechanism) so:

You might have a person object.

Customer object may have a lot of techniques like Customer.Save(), Customer.Get(int id) yet others.

These techniques don't genuinely have anything related to a person within the real life. They're really concerning the infrastructure of the application.

Repository Pattern

In repository pattern, your customer object will be a POCO, or dumb object. There are just techniques and qualities it really must represent a person (such things as title, current email address, List orders, etc.)

When you wish to persist the client - you just pass it for your repository


The Active record pattern is fast and simple to utilize. Regrettably, it will clutter your domain model with one of these techniques that do not genuinely have anything related to a person. This causes it to be slightly harder to keep your domain model with time.

For a number of situation it's very appropriate to make use of an energetic record pattern. For instance - If I am writing a reasonably simple application that's most likely not likely to change much, I'd most likely turn on SubSonic and generate my an energetic Record DAL. I'd be coding my company code within twenty minutes and all sorts of the DB stuff is taken proper care of already.

If, however, I'm modelling an especially complex domain, rich in inclination towards change, I'd rather keep my domain models clean, and implement a repository pattern with nHibernate or similar...

It has been a very long time since i have folded my very own data access using ADO.Internet, and that i don't really recommend it now you will find a lot of great data access tools available.

The "Active Record Pattern" has become a core a part of most programming frameworks. It can make simpler CRUD (Create, Update, Read, Remove) tasks faster to attain. For instance instead of needing to write many SQLs to Place, Update and Remove many common and straightforward data objects, it enables you to definitely simply assign the values towards the data object and operate a command e.g. $object->save(), the SQL is put together and performed for you personally.

Most frameworks also implement data associations inside their particular Active Record models which could greatly simplify being able to access data associated with your object. For instance, in CodeIgniter, should you specified that the Category "has numerousInch Items then after loading the course object in the database, kinds it's child Items having a simple type of code.

foreach ($category->products as $product) {
  echo $product->name;

Another advantage of Active Record is, while you say, it makes your code easily portable to various database platforms (as long as the framework that you're using includes a driver for the selected database) and even though this is unlikely to appear important at this time, it my have greater value later on in case your application becomes popular!

Hopefully this can have assisted. Wikipedia describes Active Record well (http://en.wikipedia.org/wiki/Active%5Frecord%5Fpattern) and also the CodeIgniter paperwork will aswell. Personally, I personally use KohanaPHP (http://www.kohanaphp.com) the industry PHP5 only fork of CodeIgniter and that i discover that it's ORM models are extremely helpful!