Object Relational Mapping and Active Record Design Pattern

Object Relational Mapping ORM is a programming approach to handle data between the Database and the Object Oriented Language ex: .Net , PHP5 , Ruby on Rails, Python etc. I m writing in the context of Web Applications alone.

You might have come across the term RAD – Rapid Application Development. ORM is a tool which helps rapid development.

CRUD – Create Read Update Delete is a cool technique used to speed up development and data entry.

There are many CRUD generators available for almost all platforms as form development takes up a lot of development time. Some frameworks like CakePHP call it Scaffolding. It’s not recommended for production environment though.

Some examples

Active Record Design Pattern

According to this design pattern, each database table is an object. So each Object is mapped to a database table (Relational Data) and hence O R M.

In short a database table is wrapped into a Class.

Also these concepts aid separation of business logic and database code in the application.

For instance,

Product. name=”Code Book”




The framework should fire a SQL statement internally like

INSERT into Products (id,name,price,quantity) VALUES(null,’Code Book’,20,10)

For reading the Model / Class Data , you should be able to do something like

Product.findAll() which does a query – select * from products

Product.find(condition=>[name LIKE ‘%Code%’])

select * from products where name LIKE ‘%Code%’

I would like to tell about the Dependent Models/ Classes or Parent Child relationships.

A Product can have a ProductDetail class which is stored in the product_details table

Now we would normally have a foreign key in the product_details table which points to the Product table.

In terms of Classes/ Models the ProductDetail class is a child of the Product class. or Product class is a Parent of the ProductDetail class.

So when i load the Product using

Product.find(condition=>[name LIKE ‘%Code%’]) Internally a JOIN happens on the product_details table the object would have all related data Stored or Persisted

This is a very simple case, ideally many tables might be joined on the parent table based on a Foreign Key.

Performance Issues

What’s my concern about ORM is the amount of System resources this thing is going to take in an high traffic production environment. Say I have 1 Parent table and 30 dependent tables.

Each and every time I do any manipulation on the main table (even a simple read) 31 tables are going to used.

I remember Ram telling me that some ORMs do support Lazy Loading.

CakePHP does offer this called as the Recursive Loading where you can say recursive =0,1,2

from the manual

When the $recursive option is set to an integer value greater than 1, the findAll() operation will make an effort to return the models associated to the ones found by the findAll(). If your property has many owners who in turn have many contracts, a recursive findAll() on your Property model will return those associated models.

What I feel is that ORMs are not perfect and are highly suitable for internal applications.





I am on TV talking about Mobile Blogging

NDTV wanted to do a feature on Mobile Blogging. So they contacted me through Kiruba.

The reporter and the crew came to my place on Wednesday evening after I was back from work.

I spoke about Mobile Blogging , MoMo Chennai and other related stuff.

Really excited about it 🙂

The story is up on Ndtv’s website

Hope the video will be up soon as well.

UPDATE : The Title for the program is World of Mobile Blogging

But as of 11:40 A.M IST the title I see in the bottom scroller is Blog with your Phone

The next telecast is at 12:45 PM IST




Started Working ..

College life sucks 🙁

Office life is going to be fun 😀

At least I will need to use my brains somewhat ?? ( at least more than what i have done for engineering 😛 )

I have joined this company called Photon who are mainly into web based services.

Got this offer via campus last year.

The stuff they do are mainly web based services. Java,J2EE, Flash PHP/MySQL, .Net etc.

Hoping to learn a lot of stuff at this job 🙂 ( apart from the bucks 😛 )