Alex Rivera | Logout

Is it a good idea to use CodeIgniters Active Record library to manipulate MySQL databases or should I just use SQL?

Asked 2010-03-06T22:45:23.000
16

I'm starting to get to grips with CodeIgniter and came across it's support for the Active Record pattern.

I like the fact that it generates the SQL code for you so essentially you can retrieve, update and insert data in to a database without tying your application to a specific database engine.

It makes simple queries very simple but my concern is that it makes complex queries more complex if not impossible (e.g. if need for engine specific functions).

My Questions

What is your opinion of this pattern especially regarding CodeIgniters implementation?

Are there any speed issues with wrapping the database in another layer?

Does it (logic) become messy when trying to build very complex queries?

Do the advantages out way the disadvantages?

Edit
Report

1 Answer

2

What is your opinion (sic) of this pattern especially regarding CodeIgniters implementation?

Can't say much about CI's implementation. Generally I avoid AR for anything but the simplest applications. If the table does not match 1:1 to my business objects, I don't use AR, as it will make modeling the application difficult. I also don't like the idea of coupling the persistence layer to my business objects. It's a violation of separation of concerns. Why should a Product know how to save itself? Futher reading: http://kore-nordmann.de/blog/why_active_record_sucks.html

EDIT after the comment of @kemp, I looked at the CI User Guide to see how they implemented AR:

As you can see in PoEAA an AR is an object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data. This is not what CI does though. It just provides an API to build queries. I understood that there is a Model class which extends AR and which can be used to build business objects, but that would be more like a Row Data Gateway then. Check out PHPActiveRecord for an alternate implementation.

Are there any speed issues with wrapping the database in another layer?

Whenever you abstract or wrap something into something else, you can be sure this comes with a performance impact over doing it raw. The question is, is it acceptable for your application. The only way to find out is by benchmarking. Further Reading: answered 2010-03-06T23:29:49.880

Your Answer