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”

Product.price=”20″

Product.quantity=”10″

Product.save();

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.

Links

http://manual.cakephp.org/chapter/models

http://ramsnotes.blogspot.com/2007/02/brief-introduction-or-mapping.html

http://www.martinfowler.com/eaaCatalog/metadataMapping.html

Published by

Varun Krish

Varun Krish has been dabbling with computers and websites for almost 2 decades. He has traveled to over 30 countries and hopes to visit every country on earth one day. He is currently the Editor-in-Chief of FoneArena.com and also advises startups and product companies on how to build better products. You can follow him on @varunkrish

Leave a Reply

Your email address will not be published.