• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Getting Started with ORM in PHP

By Akash Mehta | on Jun 14, 2008 | 1 Comment
PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

ORM, or Object Relational Mapping, is a database design approach that simplifies managing complex databases for programmers. Instead of direct database access, an ORM layer in a PHP framework can make “objects” stored in a database behave like actual objects from a programming perspective – for example, creating a new “car” stored in the database could involve a call to $car->new(). By abstracting actual database access, web development can be more productive and result in more reliable applications. Here’s a quick intro to ORM in PHP.

ORM brings data closer to a programming paradigm, where actual information is stored and accessed through interfaces that resemble actual objects. In PHP frameworks, this is typically achieved through the framework providing classes representing database information, that can then be manipulated as actual objects such as a car or a booking. This is almost always a level above actual database operations – when using an ORM layer, writing actual SQL queries is taken care of, although an understanding of how to is always helpful.

Let’s consider a simple database, consisting of users and posts. ORM defines relationships between objects (and in general, also between tables) where an object either has one or has many of another object. For example, a post might have one author, while an author would have many posts. By adding foreign keys to the actual database, and defining these simple relationships (usually hard-coded), an ORM layer can take care of working with related data.

CakePHP‘s ORM layer is possibly one of the most used ORM layers of any PHP framework, and is a good example of how a simple ORM implementation should function. In Cake, when an author model is defined as having many posts, any calls to fetch a particular author will automatically fetch all posts related to that author. The entire database logic is taken care of, and instead of thinking in terms of SQL queries, we get to think in terms of actual objects.

Symfony also has quite a nice ORM layer, although the symfony implementation is a little more hands-off, based around database design conventions and it’s YAML schema file approach. But you don’t need to be using a PHP framework to take advantage of ORM; the Doctrine project provides a powerful ORM layer that can be implemented on its own or within an MVC framework. Doctrine also provides an object-oriented SQL-like language to write database queries – the Doctrine Query Language is is great for complex object retrieval. Propel is another popular option, and is actually the ORM layer behind Symfony.

At the end of the day, ORM provides a fundamental advantage over writing raw SQL queries: it’s designed to retrieve information, not record sets. While thinking about data in terms of objects is a major paradigm shift from hands-on PHP scripting, it’s a great way to deal with the challenge of complex databases. So what are you waiting for? Grab a framework or a library, and dive in to the world of ORM.

watch twilight in divx

Share this story:
  • tweet

Author Description

One Response to “Getting Started with ORM in PHP”

  1. November 5, 2009

    Bora Log in to Reply

    php ORM Faces http://code.google.com/p/php-orm-persistence/

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 0Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.