Object Relational Mapping in PHP

This blog is about what is Object Relational Mapping? Why and when we should use Object Relational Mapping with PHP.
What is Object Relational Mapping?

Object-Relational Mapping in computer science is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language.
It treats data stored in relational (SQL) database table records as if they are objects. It is like a layer between Database and Application.
We use ORM in Object Oriented Approach.For each table, we create a class with member variables. These classes represent tables and it’s variables are like fields of that database tables. Whenever we need to access some tables, we can create an object of that class, assign values to member variables, and then have to call a member function of that class related to the operation which we want to perform.
How to perform an insert operation using Object Relational Mapping?
Here I am using syntax of CodeIgniter(DataMapper). Let’s assume we have a database table name “user” and it has five attributes id(Auto Increment), name, email, address and phone.
First, We will create a Class for table. We will give that Class name as our table name.

class User
{
   var $table="user";
   function User()
   {
     parent::DataMapper();
   }
}

 

Your code will be like this:

   $user= new User();
   $user->name=”Tanya”;
   $user->email=”tanya@example.com”;
   $user->address=”5D, ABC Apartment, New Delhi ”;
   $user->phone=”9898989898”;
   $user->save();

See, In the above example we did not need to write SQL queries. ORM does this for us. ORM will automatically generate SQL queries. Even we do not need to take care of type of fields.
INSERT INTO `user` (`id`, `name`, `email`,`address`,`phone`) VALUES (1, ‘Tanya’, ‘tanya@example.com’,’5D, ABC Apartment, New Delhi’, 9898989898);
Why should we use Object Relational Mapping?
The benefit of using ORM(Object Relational Mapper) is that we do not have to take care of back-end technology while writing application code. We only consider data model so that in future if we have to change back-end technology stack from one DBMS technology to another we only need to change configuration file, application code will remain same.
When should we use Object Relational Mapping?
Whenever we think for using any new approach the first thing which come in our mind that if we should use it or not. Here are some key points we should consider when taking decision to use ORM.

  • We are using Object Oriented Model.
  • We have many tables(more than 10).
  • Your application’s 80% operations are CRUD. For rest 20% you can use stored procedures. Some ORM support creating and calling stored procedure.
  • There are chances to port you project from one technology to other.
  • When project team have more than 5 developers.

What are the advantages of Object Relational Mapping?

  • Database abstraction.
  • Huge reduction in code.
  • Object Relational Mapping helps us in protecting our application from SQL Injection.
  • Our code base will be consistent. No SQL queries in the application code.
  • It gives relaxation to developer of taking care of datatype of each fields in table while writing code.
  • ORM supports to ACID properties and transactions (commit and rollback).

What are the disadvantages of Object Relational Mapping?

  • Developer needs to take care of many more objects.
  • In case relationships are not defined property, It’s a bit difficult for optimization of code.
  • Sometimes Object Relational Mapping fails with complex queries.In that case we can use Stored Procedures.

For different PHP frameworks, many ORMs are present. These are list of some popular ORMs in PHP.

In my next blog I will come up with the configuration and working of Datamapper ORM.
Thanks
References:-

  • https://en.wikipedia.org/wiki/Object-relational_mapping