views:

41

answers:

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  A: 

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.

Sarfraz
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)
Calle