Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM and Active Record Pattern in PHP?

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?

like image 694
Tower Avatar asked Jul 11 '09 13:07

Tower


People also ask

What is PHP Active Record pattern?

The Active Record pattern effectively prescribes to wrap a row of a database table in a domain object with a 1:1 relationship, managing its state and adding business logic in the wrapping class code.

What is active record ORM?

1.3 Active Record as an ORM FrameworkRepresent models and their data. Represent associations between these models. Represent inheritance hierarchies through related models. Validate models before they get persisted to the database. Perform database operations in an object-oriented fashion.

Which ORM is used in PHP?

The Doctrine Project is the home to several PHP libraries primarily focused on database storage and object mapping. The core projects are the Object Relational Mapper (ORM) and the Database Abstraction Layer (DBAL) it is built upon.

What is active record in laravel?

What is ActiveRecord? Active record is an architectural pattern, named by Martin Fowler in 2003, that stores in-memory object data in a relational database. The interface for this pattern includes CRUD methods and properties that map, more or less, one on one to the column in the underlying database.


2 Answers

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
like image 55
Jani Hartikainen Avatar answered Oct 13 '22 12:10

Jani Hartikainen


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

  • Are there good reasons not to use an ORM?
  • Using an ORM or plain SQL?
like image 21
KahWee Teng Avatar answered Oct 13 '22 12:10

KahWee Teng