1

Ok so again my question is kind of a best practices/techniques question so I'm assuming there will be a few different ways to handle this.

So, to what degree should I factor in error handling? As an example, say I havee a basic function in my model which retrieves a customers record ie

function get_customer($customer_id) {

    $this->where('id',$customer_id);
    $query = $this->db->get('customers');

    return $query->result();

}

Should I put in something to check whether the parameter $customer_id, exists and is the correct type? or does it depend on the logic of the application? For instance, in some cases this function would never be called unless $customer_id is set and that might be checked in the controller, but is it best practice to include the error checking here as well?

Also, should I use something like try/catch and throw an exception or can it just be as simple as if ($customer_id!='') etc ?

1 Answer 1

1

You are using the above function from a controller right? The best place to make sure that the variable $customer_id is of correct type and contains what you expect, you need to check it from within the controller before sending it to the model. The controller puts you in a position to show the error by redirecting or whatever.

Sign up to request clarification or add additional context in comments.

2 Comments

which would be another way of saying that error-checking is always supposed to be done only as neccessary? e.g. if the variable can be supposed to be of the right value type, it may come from a select field, we do not need to check it? But even then, if we do need to check it having it done in the controller sure would create a lot of redundant code. Lean controller / fat model - fans would not agree to your solution then... or? (asking because I don't know about this myself)
two things, just because POST data comes from a <select> box doesn't mean you don't have to check it. it is trivial for a knowledgable user to send info in that field that wasn't in your original <option>s. while the form_validation library is great in the controller and should be used, you should definitely have validation in your model as well. models should be reusable, possibly across many controllers, so don't rely on the controller to always sanitize/validate the data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.