Page 2 of 2

Posted: Mon Feb 26, 2007 9:37 am
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.

Posted: Mon Feb 26, 2007 7:32 pm
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;
    }

Posted: Tue Feb 27, 2007 3:56 am
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.

Posted: Tue Feb 27, 2007 4:26 am
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.

Posted: Tue Feb 27, 2007 4:30 am
by Maugrim_The_Reaper
What's that pseudo SQL language used by Hibernate and Propel (PHP)? ;)

Posted: Tue Feb 27, 2007 4:45 am
by Christopher
HQL, but you might also be thinking of OQL.