Can someone tell me the advantage of using ActiveRecord vs writing a plain sql query?


Reference: EE Database Class User Guide

The database class (IE CodeIgniter's ActiveRecord, which really isn't ActiveRecord, reference) does some nicities for you, like escaping your queries automatically. It also allows you to reference your table names without using whatever Expression Engine table-name-prepending that occurs at install (usually something like "exp_table_name"). See the reference.

It definitely makes your code cleaner to read, and I only not use it when I have to do some seriously complex SQL stuff, like building multitable search queries. I recommend it for its code cleanliness highly.

Also what @johnathan-waters said. Although I feel that this wouldn't apply to ExpressionEngine as it is built around MySQL, and I do not know if you can easily switch DB adapters for an EE install.

  • 1
    Very true. It also future proofs your code. PHP has switched to the MySQLi structure and EE will update with the new standards as they come out. So if they decide to change again, you won't have to update your code. – Johnathan Waters Dec 23 '14 at 18:46

From the CodeIgniter docs:

Beyond simplicity, a major benefit to using the Active Record features is that it allows you to create database independent applications, since the query syntax is generated by each database adapter. It also allows for safer queries, since the values are escaped automatically by the system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.