|
Introduction
ORM (Object Relational mapping) is a technique in which Objects oriented languages i.e Objects are mapped directly to relational databases. This is a generic concept which can be applied to any object oriented language like C++, Java, PHP etc and any relational database like Oracle, Mysql etc.
Object Relational Mapping Frameworks
Generally if you are working in a big web application you will have the data stored in some relational database. For e.g. If you are using PHP to build web applications there is a good chance that you would be using MySql as your relational database. As the application gets large dealing in with SQL in your PHP code gets very messy.
ORM frameworks are generally written in an object oriented programming language like PHP and map (or even generate) objects which directly map to relational databases.
So if you have an ORM object of customer and want to save it in your database the code required to do that would be some what like the following if you use an ORM frame work
$objCustomer = new Customer();
$objCustomer->name=”John”
$objCustomer->emaail=”
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
”;
$objCustomer->save();
Some of the good Object Relational Mapping Frameworks in PHP
CakePHP
CakePHP is a rapid application development framework in PHP. You can download it from http://cakephp.org/ .
CakePHP has a support for developing application with ORM and MVC architecture. The licensing of CakePHP is flexible as it is distributed under MIT license. CakePHP is a complete Object oriented framework.
As CakePHP is a complete MVC architecture supporting framework it is an excellent framework to use specifically if a new application is to be started. CakePHP also has a good mechanism to define relationships between different objects and tables.
CakePHP has a feature called as CRUD scaffolding which with one line helps you create views for performing CRUD (Create Read Update Delete) operations once your modeler is defined.
Doctrine
Doctrine is another good ORM framework in PHP. It can be downloaded from http://www.doctrine-project.org/ .
Doctrine allows one to write database query in Doctrine Query Language (DQL) which is a dialect of SQL which is inspired by Hibernates HQL. This provided one with the flexibility to easily change backend database without much rework. Doctrine also has support for hooks. Hooks can be used to validate data which is stored in the database or which is retrieved from the database. Doctrine also has other features like a caching framework and also the ability to combine multiple PHP files in one which reduces the performance which is due to inclusion of many PHP file at runtime.
Propel
Propel is an ORM toolkit written in PHP. It can be downloaded from
Propel is an open source framework which is under which is released under the MIT license. Propel has a component for source code generation which generated PHP classes on basis of datamodel definition language which is in XML. Propel also has a component which manages connections and transactions with RDBMS you are using.
Propel has a features of easily describing relationship between different objects .It also has features to define database transaction in your code which will help you ensure database integrity and also improves the performance of database queries.
CoughPHP
CoughPHP is only an ORM framework in PHP. It can be downloaded from http://coughphp.com/
CoughPHP is not an MVC framework and hence is very light weight compared to other PHP frame works which provide MVC functionalities as well .As CoughPHP is not an MVC framework it does not affect the ways your views are built .Because of this it can be integrated into existing projects also easily . It is an open source framework and is licensed under FreeBSD license.
CoughPHP generates all the PHP classes for the ORM mapping to the database. CoughPHP also provided hook like mechanism to validate or invalidate data before saving them in database. It also has support for specifying database transactions.
Symphony
Symphony is a MVC architecture web application framework written in PHP. It also has support for ORM .It can be downloaded at http://www.symfony-project.org/
Symphony is an open source framework which is licensed under MIT license. Symphony uses Propel or Doctrine as its Object Relational mapping layer. So if you are using Symphony you can use all the features which belong to Propel or Doctrine. As Symphony is a complete MVC framework it helps you create views controllers etc also. Symphony makes heavy use of other open source frameworks like Zend , Propel , Doctrine. So the features of these frameworks can be used when using Symphony. JavaScript frameworks like jQuery and prototype can be easily integrated as plug-ins in Symphony.
Advantages of using a PHP ORM framework.
Most PHP ORM frameworks generate code which is required for CRUD ( Create Read Update Delete ) operation on the database automatically . This helps in rapid development of the application as one requires to deal with only high level things of the application. The code for basic database functionalities is directly taken care by the PHP ORM framework
In relational database you have a type like int, text, blog etc associated to each field in the database. Some times we have to convert the type on the fly from get data or storing data in the database from PHP code. This all will be taken care by the PHP ORM framework which is used in the project.
As the PHP ORM frameworks are used by many people they are less likely to have security problems than hand written code specifically for your project. This protects your application from attacks like SQL injections etc.
By using ORM frameworks there is no mixing of SQL code with business layer code of your application. This makes the code much easier to maintain.
If during the lifecycle of the application the backend database needs to be changed then if a PHP ORM framework is will become comparatively much easier to perform a transition then if one was not using a ORM framework.
|