Search This Blog


Object Relational Mapping (ORM)

Well, for my first post about technology, I decided to write about something high level and, suddenly, that is not so known between PHP programmers: ORM. I expect, initially, to show the concept and maybe, in future posts, inspect it with more acuracy.

ORM is the acronym of Object Relational Mapping. It is a technique used in OOP that uses Relational Databases (that are still more frequently used, while Object Oriented Databases are growing). The technique consists to map each table of database in application classes. In the same way, each column of a table of database are mapped into an attribute of their application class.


We have the follow table in our DB:

  username VARCHAR(50) NOT NULL DEFAULT '',
  password VARVAR(32) NOT NULL DEFAULT '',

In application, we could have the follow class, that represents "user" table:

class user extends ORMBase {
    protected $id;
    protected $username;
    protected $password;



I do not know, exaclty, the main target of this technique, but I can list that as relevant:

  • Unify the DAO layer. It allows that queries in DB be done dinamically, through class methods. In that way, you do not need to write SQL manually.
  • Make the development faster, because the code became at more high level.
  • Can make the development more secure, because the layer responsible for ORM can treat some questions like SQL Injection, that is a kind of security fail.


I know that PHP has third classes that supports ORM, however I have never used anyone. By the way, I know the concept because I have implemented a ORM layer by myself. There are no doubts that implementing an ORM layer is not trivial, especially if that implementation could suport more than one model of Database. As each DBMS has its owns features, data types and syntax, implementing something that makes SQL according to the DBMS is a complex task. Besides the basic mapping cited above, it is necessary to make a type mapping, in other words, the data type used by the DB should be mapped into a PHP data type. For example: VARCHAR (in DB) could be string at application.

The complexity increases when we include the relationship between tables (foreign keys). In that case, we have interesting situations: for example, a class that represents a table with a relationship can have an attribute that represents the key of the relationship (the value of the foreign key) and can have an attribute that "points" to the original record (this attribute is an object of the class that represents the relational table).

For example, it whe have a table "teacher" that has a relationship with the table "person", I could have a code like that:

$teacher = new teacher();
$teacher->formation = 'Biology';
$teacher->person->name = 'John Brown';

Well, I think my implementation of ORM was one of the most complex code I have implemented. In any case, it helps so much. Do you ear about Java's JPA? It is a data persistency framework that uses the ORM concept.

No comments:

Post a Comment