2

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

2 Answers 2

3

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
  • 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. Dec 23, 2014 at 18:46
2

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 and acknowledge you have read our privacy policy.

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