Object Relational Mapping

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Indeed, it's also become apparent why there are quite large frameworks available for ORM! (It was mostly this that had me intrigued - "why would something like that need a large framework?")
Which also explains why ActiveRecord has been hyped to death by everyone since RoR was released ;). Personally I prefer a simple Data Mapper or Data Access Object implementation since it needs less code up front when you just have a few tables, but real ORM is very useful on large projects where a large ORM framework becomes less of a performance/KISS concern and lets you deal with dozens of objects more elegantly.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Jenk wrote:I'm getting confused now though, because I just spotted a PHPBuilder tutorial that shows my first example to be more correct than the latter :\

http://www.phpbuilder.com/columns/mathi ... 40309.php3

(Though I am more inclined to believe the tutorial is mistaken (or rather my take on it) as it makes perfect sense that my first example was/is an active record.. it's a single record, and it's active.. tada!)
That tutorial is also showing Active Record (and it's from 2004). And the examples that you and Maugrim showed are also Active Record. Which makes me think that Active Record is probably really the solution you are looking. I should note that Active Record often has some O/RM like functionality added. For example, Rail's Active Record generates its mappings through convention -- which is a pretty common scheme.

Real O/RM can be a great solution, but you need to have a problem that it solves. Usually that problem is that the database schemas are out of your control and change often enough to be a problem. That's not the reality of most web applications.

Also, a comment on your code. It really should track objects by type so it can save them, but it does not need to store them like a Registry so they can be retrieved. So:

Code: Select all

class Mapper
{
    private $_objects = array();

    public function getInstance ($class)
    {
        // create instance
        $obj = new $class;

        // save reference so we can save it later (by class so we know which mappings to use)
        $this->_objects[$class][] = $obj;

        return $obj;
    }
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I'm not looking to solve a problem, I'm looking to gain an understanding :)

By what you're saying, the difference between an OR mapper and an Active Record is whether the object saves itself or not.. regardless of the actual mapping element provided by a 3rd object.

In short, my understanding is as follows:

ORM:
An object is in disregard for what, where and how it's state and/or data will be saved. A mapper will take care of that.

Active Record:
This object will save it self. (Hallelujah, another [s]soul[/s] object saved!) It has a direct 1 object to 1 record relationship.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Yes. :)

The interesting thing about both Active Record and O/RM is that they definitely suffer from the law of diminishing returns. Each additional bell and whistle you try to add to them just makes them worse. They are best kept simple -- especially in PHP.
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

What's that pseudo SQL language used by Hibernate and Propel (PHP)? ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

HQL, but you might also be thinking of OQL.
(#10850)
Post Reply