Tuesday, October 11, 2011

How to Implement ORM on PHP5

How to Implement ORM on PHP5:

ORM (Object-relational mapping) programming is the best way to improve your PHP skill. There are many ORM framework in the market. However, CakePHP, Symfony and Doctrine are the best in the crowd. ORM layer in a PHP framework can use “objects” stored in a database behave like actual objects from a programming perspective. For example, creating a new “mobile” stored in the database could involve a call to $mobile->new(). ORM brings data closer to a programming paradigm. It is the manipulation of object rather than the data.

ORM works one level above actual database operations. During the use of ORM layer, you don’t need to write SQL queries. It is taken care of, although an understanding of how to is always helpful. ORM works based on MVC structure. Symfony is possible the most robust ORM layer. However, CakePHP‘s ORM layer is not far away. Doctrine is also quite competitive, in providing ORM. There are many other frameworks. Of course, you can to do experiments on their ORM.  Finally, ORM provides many fundamental advantage over raw SQL.  It massively simplifies the interactions with your database and eliminate the chore of hand written SQL for common operations. During programming, most of the time you will be using code generators or mapping files to manipulate your tables.

Typical ORM Conventions:
To get the most value out of the ORM library, you should adhere to the conventions outlined below. Moreover, it is possible to override these default conventions via ORM object properties.
  • Table names are plural, e.g. users .
  • Model names are singular (e.g. user)
  • Each table should have an auto_incrementing column named ‘id’ as its primary key)
  • Foreign keys should be named using the ‘modelname_id’ (e.g. user_id)
  • Pivot tables should use the parent table names in alphabetical order in the form table1_table2. For example: If you have a many-to-many relationship between users and roles tables, the join table should be named roles_users.
ORM Relations:
The ORM library supports the following relationships in your model:
  • hasOne for one-to-one relationships
  • hasMany for the parent side of a one-to-many relationship
  • belongsTo for the child side of a one-to-many or one-to-one relationship
ORM Coding (Example):
Using code generators (like cake bake),  you easily generate codes for ORM. But discussion purpose some examples are given below:

Example:
Step 1: Create the Database on MySQL

CREATE TABLE `xyzs` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` varchar( 60 ) NOT NULL ,
`address` varchar( 260 ) NOT NULL ,
`email` varchar( 60 ) NOT NULL
);
CREATE TABLE `pqrs` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`code` varchar( 40 ) NOT NULL ,
`title` varchar( 40 ) NOT NULL ,
`description` text NOT NULL ,
`xyz_id` int( 11 ) NOT NULL
)

Step 2: Define Models
Create the Xyz model using the following code (/app/models/xyzs.php):
<?php
class Xyz extends AppModel
{
var $name = ‘Xyz’;
var $hasMany = ‘Pqr’;
}
?>

Create the Pqr  model (/app/models/books.php):
<?php
class Pqr extends AppModel
{ var $name = ‘Pqr’;
var $belongsTo = ‘Xyz’;
}
?>

Step 3: Define the Controllers
Create a controller for the Xyz model with the following code: (/app/controllers/xyzs_controller.php):
<?php
class XyzsController extends AppController {
var $name = ‘Xyzs’;
function index() {
$this->Xyz->recursive = 1;
$authors = $this->Xyz->find(‘all’);
$this->set(‘xyzs’, $xyzs);
}
}
?>

Create the controller for the Pqr  model (/app/controllers/books_controller.php):
<?php
class PqrsController extends AppController {
var $name = ‘Pqrs’;
function index() {
$this->Pqr->recursive = 1;
$authors = $this->Pqr->find(‘all’);
$this->set(‘pqrs’, $pqrs);
}
}
?>

Step 4: Define the Views
The view file for the action for both the tables should be like this: (index.ctp):
<?php foreach($xyzs as $xyz): ?>
<h2><?php echo $xyz['Xyz']['name'] ?></h2>
<hr />
<h3>List of PQR(s):</h3>
<ul>
<?php foreach($xyz['Pqr'] as $book): ?>
<li><?php echo $pqr['code'] ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>

No comments:

Post a Comment