Welcome Guest, Not a member yet? Register   Sign In
Should I use SQL, the Active Record class or even an ORM
#1

[eluser]Kip zonder Kop[/eluser]
For the sake of gathering as much information as possible and gaining new insights I'll act a little more ignorant then I actually am.

Suppose one speaks fluently SQL and is completely comfortable with it. Then,

1) What do you think are the advantages and drawbacks of using the Active Record class as opposed to self written SQL?

2) What do you think are the advantages and drawbacks of using an ORM as opposed to self written SQL?

I'm asking this to finally decide whether I should invest in learning AR and DMZ, Doctrine or Rapid Data Mapper.

Thank you.
#2

[eluser]jedd[/eluser]
[quote author="Kip zonder Kop" date="1262040051"]
Suppose one speaks fluently SQL ...
[/quote]

Fluent SQL, do you mean? Wink

Quote:1) What do you think are the advantages and drawbacks of using the Active Record class as opposed to self written SQL?

AR promises DB-agnostic code, which is handy if you think you'll ever change DB's. In general, most people do not.

AR is arguably easier to read for other people coming to maintain your code.

AR gives you an extra level you have to debug.

Apart from relatively trivial applications, at some point you are likely to need to drop into 'raw' SQL - at which point the agnostic aspect becomes less clean.

AR automatically escapes your data - this is easier than using query bindings (though not by much) or db->escape() (a bit more).


Quote:2) What do you think are the advantages and drawbacks of using an ORM as opposed to self written SQL?

I don't use an ORM, but it seems to me that you get yet another layer that things can go wrong in - ie. another thing you (might) need to debug.

There seems to be very few people who start a new application from scratch, and think they want ORM, and go down the OO DB path - instead sticking with a conventional RDBMS with an ORM wrapper. I've never understood this trend.

ORM's are likely to have performance implications (though probably overestimated).
#3

[eluser]Cro_Crx[/eluser]
The main advantages of using Active Record and using an ORM is that you will be able to achieve the same objective while writing less code.

Less code == Less bugs. I've heard that on average, for every 100 lines of code you write 25 of them will be incorrect (as in will be improved or rewritten later) and you'll have at least 1 bug.

Initially using ActiveRecord may even be slower than writing SQL as you won't be used to it, after a while it's almost quicker and takes less effort. Using an ORM has given my application building time a big decrease. Plus the models are really neat.
#4

[eluser]Kip zonder Kop[/eluser]
@jedd

Thank you for your elaborate answer.

Quote:Fluent SQL, do you mean? Wink

I suppose so. Dutch is my native language so sometimes ...

Quote:There seems to be very few people who start a new application from scratch, and think they want ORM, and go down the OO DB path - instead sticking with a conventional RDBMS with an ORM wrapper. I've never understood this trend.

That's an interesting observation. But then again, what are easily accessible OO DB's for a PHP programmer.
#5

[eluser]Kip zonder Kop[/eluser]
@Cro_Crx

Quote:
Less code == Less bugs. I've heard that on average, for every 100 lines of code you write 25 of them will be incorrect (as in will be improved or rewritten later) and you'll have at least 1 bug.

Using an ORM, I will maybe write less code but there will be a whole lot of code between my code and the database layer that wouldn't have been there if I had decided to use plain SQL. That's jedd's debug argument I guess. (see above).
#6

[eluser]jedd[/eluser]
Ah - your English is so good I assumed you were a native speaker.

As Cro_Crx alludes, commonly shared code (the ORM code base) is going to be debugged by more people than your application code - so while it's true that all code may contain bugs, there's probably an argument to be made there that the ORM code is likely less buggy.

OTOH any code you didn't write yourself is generally harder to debug. It might also be fair to say that for many users of an ORM suite, the complexity of the ORM code is greater than they are comfortable with.

As to examples - well, db4o, but it doesn't appear to have a PHP interface. Wikipedia contains a bunch of links to object(ish) databases. Even postgres is cited, though I gather it's some way off being 'fully featured'. It's weird, though, again - that preference for MySQL with an ORM wrapper, rather than something that's a bit more OO-ish off the shelf.




Theme © iAndrew 2016 - Forum software by © MyBB