Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Active Records?

I'm tinkering with CodeIgniter and have come across Active Records for the first time. At first I dismissed it as something for people who don't really know how to write SQL. I realise now that my analysis was flawed and Active Records are pretty prominent, especially in Rails.

But what purpose do Active Records hold? Is it to abstract away from different RDBMS individualities. If so I thought that isn't that what SQL is meant to do. Furthermore what is best practice, should I be using these?

Thanks in advance

like image 455
zenna Avatar asked Sep 10 '09 10:09

zenna


People also ask

What is the difference between an active and inactive record?

Active records are referenced at least once a month, and are usually maintained in a readily-accessible spot in the office space or office systems. Inactive records are no longer used on a regular basis during the course of business.

What are examples of Active Records?

Active records are those in which the person on the record has had some sort of dealings with the business fairly recently. For example, if you went to the dentist last week or even a few months ago, then your record would be considered active.

What is Active Records in medical terms?

Active Medical Records means all medical records (maintained electronically and/or non-electronically) relating to patients who have been seen or treated by Lucid or a Lucid Subsidiary in the two (2) year period prior to the Effective Date, but excluding records of terminated or deceased patients; Sample 1Sample 2.

What is Active Record in an organization?

A record documenting those activities of an organization that are directed towards the substantive purpose for which the organization was created. 2. A record that assists an organization in performing its primary function.


2 Answers

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

At the moment there are two main design patterns I seem to come across regarding data access: ActiveRecord and the Repository Pattern

Active Record

Your objects contain methods for persisting their state to a DB (or other persistence mechanism) so:

You may have a Customer object.

Customer object will have a bunch of methods like Customer.Save();, Customer.Get(int id); and others.

These methods do not really have anything to do with a customer in the real world. They are really about the infrastructure of your application.

Repository Pattern

In repository pattern, your customer object would be a POCO, or dumb object. It only has methods and properties that it really needs to represent a customer (things like name, email address, List orders, etc.)

When you want to persist the customer - you simply pass it to your repository

Repository.Save(MyCustomer).

The Active record pattern is quick and easy to work with. Unfortunately, it does clutter up your domain model with these methods which do not really have anything to do with a Customer. This makes it slightly harder to maintain your domain model over time.

For a lot of situation it is very appropriate to use an Active record pattern. For example - If I'm writing a fairly simple app that is probably not going to change much, I'd probably fire up SubSonic and generate my an Active Record DAL. I'd be coding my business code within 20 minutes and all the DB stuff is taken care of already.

If, on the other hand, I am modelling a particularly complex domain, with high susceptibility to change, I'd rather keep my domain models clean, and implement a repository pattern with nHibernate or similar...

It's been a long time since I rolled my own data access using ADO.Net, and I don't really recommend it now there are so many great data access tools available.

like image 104
Paul Avatar answered Sep 18 '22 15:09

Paul


The "Active Record Pattern" is becoming a core part of most programming frameworks. It makes simpler CRUD (Create, Update, Read, Delete) tasks quicker to achieve. For example rather than having to write many SQLs to Insert, Update and Delete many common and simple data objects, it allows you to simply assign the values to the data object and run a command e.g. $object->save(), the SQL is compiled and executed for you.

Most frameworks also implement data relationships within their respective Active Record models which can greatly simplify accessing data related to your object. For example, in CodeIgniter, if you specified that a Category "has many" Products then after loading the Category object from the database, you can list it's child Products with a simple line of code.

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

Another benefit of Active Record is, as you say, that it makes your code easily portable to different database platforms (so long as the framework that you are using has a driver for your chosen database) and although this is not likely to seem important right now, it my have greater value at a later date if your application becomes popular!

Hopefully this will have helped. Wikipedia describes Active Record well (http://en.wikipedia.org/wiki/Active_record_pattern) and the CodeIgniter docs will aswell. Personally, I use KohanaPHP (http://www.kohanaphp.com) which is a PHP5 only fork of CodeIgniter and I find that it's ORM models are very useful!

like image 23
Matt Avatar answered Sep 18 '22 15:09

Matt