Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Problem

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', "");

produces

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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