Advantages vs. Disadvantages of ActiveRecord

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

Post Reply
jaycob
Forum Newbie
Posts: 1
Joined: Sun Jun 24, 2007 1:42 pm

Advantages vs. Disadvantages of ActiveRecord

Post by jaycob »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello!

I've been trying out some ActiveRecord/ORM implementations (Propel, Doctrine ...) recently and somehow came to the following result.

The disadvantages are that ..
... whenever it comes to complex queries it's not possible to use these tools or they make everything even more complex.
... instead of quickly defining tables in well-known SQL syntax this has to be done in PHP itself or in some other language like XML.
... I can't tune my queries, although optimization is not needed very often, I was faced with some cases where it was and these ORM frameworks where simply not flexible enough
... although the standard queries are RDBMS independent you often need to specify additional conditions which then are not!
... to be continued ...

The advantages I see are
... that simple updating/deletion/selection is very easy and makes the code more readable as you don't have to analyse the sql query, instead you just see a function like ->save()
... it looks cool and is RDBMS independent, however how often do you really change you database system and there are really only a few applications that require this.
... it can make development really fast (but sometimes also slow down)
... to be continued ...

Although I'm new to devnetwork I saw that there are some really intelligent and experienced people knowing what they are talking about . So I would like to hear you opinions and experiences with ORMs please.

Is it worth using such tools and what do you think about the way Zend_Db is implemented. I personally think it just sucks because it's half sql and half orm, but not really something .

e.g.

Code: Select all

$data = array(
    'created_on'      => new Zend_Db_Expr('CURDATE()'),
    'bug_description' => 'Something wrong',
    'bug_status'      => 'NEW'
);

$db->insert('bugs', $data);
Cheers,
Jacob


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The short answer is that it is worth using one of those tools if it easily solves an problem you have. Otherwise. as you have discovered, they are more trouble than they are worth. It has been mentioned many times that O/RM suffers from the law of diminishing returns. The more features you need the more the code grows astronomically -- and flexibility does not necessarily improve.

I do want to clarify that ActiveRecord and O/RM are two different things that have sometimes been mixed -- most famously recently in RoR. They are two very different things and generally when mixed the O/RM returns an ActiveRecord object. They can be used independently. I have found that specific purpose O/RM systems can be very powerful in the right circumstances. I tend to not use ActiveRecord because I prefer a Model object returning Value Objects instead of the combined approach. But I have had cases where adding business logic to the record data is an excellent solution...
(#10850)
Post Reply