In Codeigniter (PHP framework), I am trying to create a helper function that will inject an ActiveRecord (for MySQL) command into a chain of existing ActiveRecord calls. The reasoning for this is that the current ActiveRecord commands available are not reliable enough, and produce differing results depending on what parameters are passed in.

For example:

$param = "";
$this->db->where('field', "");


WHERE `field` = ""

This would provide a result set where only blank fields were returned.

This is not always the intended behavior, as the input comes from a web form, and may be the result of no specific query being submitted - thus all results should be returned instead.

Attempted solution

What I decided to come up with, was a helper method that would take in a set of unknown inputs, and return a result set that would match my desired behavior (eg. return all results or return none).

Using test cases and a test method to abstract the desired set of inputs and outputs away, I hoped to produce an easy-to-call method for coercing the database results into my desired behavior. This would be easier then say, coercing the input into an expected type, and then using the unreliable behavior of Codeigniter's ActiveRecord commands.

My main concern however, is that my helper function may be trying to do too much - my colleague described it as a magic method that takes anything and may have arcane implementation details. It would thus be less readable than the ActiveRecord convention (which is more well-known) and harder to maintain.

Is my function the best way to go about it, or are there better alternatives out there?

1 Answer 1


Active Record is just a CRUD pattern... it maps tables and columns in a database to corresponding data structures in a programming language, and provides Create, Read, Update and Delete mechanisms. It has little to do with business logic, which is what you're describing.

Every method is a "magic" method, according to your colleague's interpretation. A method is a black box; you pass it parameter data, it returns some corresponding output. The logic in-between can be almost anything.

enter image description here

Once the logic of that method is proven, you shouldn't have to be concerned with its internal implementation. In fact, that's the basis for test-driven development: you should be able to change the underlying implementation and, if your tests still pass, your application should still exhibit the same behavior.

So to be clear, your helper method is more of a repository or service method. It really doesn't have much to do with Active Record, except that Active Record may be used by your method to retrieve the necessary data.

This is the whole point of methods, to encapsulate complex behavior. Your approach is totally sound.

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.