14

There are two things that seem to be popular nowadays and I was wondering what are the pros and cons of using something like this: http://codeigniter.com/user_guide/database/active_record.html ?

Another thing is ORM (Doctrine for instance). What are the benefits of using these?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Tower
  • 98,741
  • 129
  • 357
  • 507

3 Answers3

20

ActiveRecord is a pattern common in ORMs. Doctrine is an ORM which uses an ActiveRecord'ish style.

Some benefits of using tools like Doctrine:

  • Database independence: The code should be easy to port to different DBs. For example, I often test using SQLite and use MySQL or Postgre in production with no changes in code.
  • They reduce the amount of code you have to write: A large part of application code deals with communicating with the database. An ORM takes care of most of that, so you can concentrate on writing the actual app.

Of course, they don't come without disadvantages:

  • Doctrine is heavy so it is slower than using straight SQL
  • ORMs can be complex, adding some weight to what you have to learn, and they can sometimes be difficult to understand for inexperienced programmers
Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
  • I don't know what you mean by "ActiveRecord'ish", but here is a proper description: "Doctrine 2 is an object-relational mapper (ORM) for PHP 5.4+ that provides transparent persistence for PHP objects. It uses the Data Mapper pattern at the heart, aiming for a complete separation of your domain/business logic from the persistence in a relational database management system." Source: http://doctrine-orm.readthedocs.org/en/latest/tutorials/getting-started.html#what-is-doctrine – ZeeCoder Sep 13 '15 at 11:04
  • 1
    @ZeeCoder I might be mistaken but considering I wrote this answer in 2009, I think they didn't have Doctrine 2 out yet. Doctrine 1 had a much more ActiveRecord-styled API than their second (and much much better, I should add) version – Jani Hartikainen Sep 14 '15 at 22:21
  • My bad then. I only ever used Doctrine 2 :) – ZeeCoder Sep 15 '15 at 06:50
6

You can take a look at these questions though they're not exactly PHP specific:

Community
  • 1
  • 1
KahWee Teng
  • 13,658
  • 3
  • 21
  • 21
2

I tried to keep it light-weight and understandable. Even comes with it's own Mootools based Class Generator :)

http://www.schizofreend.nl/Pork.dbObject/

check it out :)

SchizoDuckie
  • 9,353
  • 6
  • 33
  • 40