Object Relational Mapping (ORM) integrate object programming language capabilities with relational databases, managed by different relational database managers like MySql, Oracle etc.
ORM has information about the object model in our application and knows how to transfer or map object instances data into our relational database structure and vice versa.It hides and encapsulate the changes made in the data source; whenever data sources or their correspondign API changes one only needs to change the ORM and not the application which uses ORM to data transformation.
Thus we can say that ORM is a technique for data conversion between two different type of systems with respect to how they handle data in their respective systems.
While developing the applications using Object oriented language like Php etc, we deal with objects and their instances to play with the data. And in most of the time we store the data on a permanent storage using some relational DBMS like MySql etc.
The root cause is, objects can't be directly saved to and retrieved from a relational database. Objects have their respective identity, state, and behavior in addition to data associated with them while an RDBMS stores data only. And there is no direct mapping between Objects and RDBMS data types, hence we need a method for Object- Relational integaration; Which could help us to map object with RDBMS to ease our work of data transfomation between Objects and RDBMS.
While dealing with objects a developer need to convert the object values into groups of simpler values for storage in the database and convert them back into objects type upon retrieval. And we also need to change data transformation method if/ever we have different data storage source, i.e.for each type of storage type separately.The ORM perform the same work, therefore instead of writing own method of data transformation one can use the pre-developed and well tested available ORM for the languages we are using.
Apart from the data transformation work an ORM has support for various DBMS.
It has capability to provide its own validation for the various data which can be very handy.
There are different programming languages to go with and similarly we have different ORM to choose with respect to our chosen language. For Php we have some well proven and tested ORM to go with. Some of them are Doctrine, Propel, Rocks etc
In this section we are going to have some light on the Doctrine :
Doctrine is an ORM for PHP 5.2.3 + which does not require any external libraries to run and it uses PDO (Comes officially bundelled with PHP) for database function call abstraction.
Hence, before using doctrine in the application one needs to confirm whether installed PHP has version 5.2.3+ and PDO is installed.
Doctrine has two main layers; first is DBAL (Data Base Abstraction Layer) and ORM. The DBAL is responsible to interact with PDO (Basic Cross-Databae API) and completes and extends the database abstraction provided by the PDO.
The main concept of Docrine is the Doctrine Query Language (DQL). By using the terminology of domain model: class names, fields name, relation between classes, etc. it expresses queries for an object or for a full object graph.
After installing the Doctrine a bootstrap file should be created for various initialization purpose of the ORM. The main part of this bootstrap file is the creation of connection parameter;
For connection in doctrine we have different appraoches like initializing a new PDO instance.
$dsn = 'mysql:dbname=test;host=localhost';
$user = 'username';
$password = 'password';
$dbh = new PDO($dsn, $user, $password);
$conn = Doctrine_Manager::connection($dbh);
but directly passing a PDO instance to Doctrine_Manager::connection() will not allow Doctrine
to be aware of the username and password which is required by Doctrine to be able to create and drop databases, hence we explicitly need to set username and password for connection object
$conn->setOption('username', $user);
$conn->setOption('password', $password)
The second approach is lazy database connection which should be preferred.
$conn = Doctrine_Manager::connection('mysql://username:password@hostname/dbname');
This is callled lazy connection since, Doctrine will only connect to database when needed.
Configuration for Doctrine can be defined for various levels viz for global level, for connection level or for table level.
Afer making initial configuration (installation, configuration, connection buildup etc) the main work is to define model to be used by the Doctrine for the application. And for developers sake Doctrine has support for generating doctrine_record claasses from existing database. Thus it reduces manual work to write all the doctrine_record classes for the domain model.
To genetrate model Doctrine provides its method :
Doctrine_Core::generateModelsFromDb('models', array('connection_name'), array('generateTableClasses' => true));
where models:the directory where the generated record files will be written to
connection_name : name of the connection for which model is to be build
and from the created/existng models we can create YAML schema file to manage models and generate PHP classes from them.
To be able to use all model classes we need to load the classse and for which we have Doctrine_Core::loadModels('/path/to/models'); this segment should be added in bootstrap file If we want we can go vice-versa, i.e we can create our database using availabe model classes and YAML schema file.
Now once the models have been defined we can perform the various query for the same. Some of the examples for the sake of simplicity are as follows;
Select query
$q = Doctrine_Query::create()
->select('field_name')
->from('class_name')
->where('condition');
To see the query generated we can have
echo $q->getSqlQuery();
to execute the query
$res = $q->execute();
for insert data
$table = new table_class();
$table->property1 = 'value1'
$table->property2 = 'value2'
$table->save();
for updation
$q = Doctrine_Query::create()
->update('table_class')
->set('field_njame','value')
->where(condition);
$q->execute();
for deletion
$q = Doctrine_Query::create()
->delete(table_class)
->where(condition);
$q->execute();
These are some basic DQL(Doctrine Query Language) examples, for more detail we can refer to its available resources
Hence, when we have a need to map object data with DBMS data or/and need to write multi DBMS based query or need to write a query in a less complex way one can go for a suitable ORM.
For Further reading on Doctrine http://www.doctrine-project.org can be accessed


