Welcome Guest, Not a member yet? Register   Sign In
Should Models output application errors?
#1

[eluser]the real rlee[/eluser]
Hi guys, was wondering if someone could explain this to me. Its my understand in MVC that Models should be used for data acquisition only - they should be outputting anything. My issue is what about application errors? Like say i have a get_user_by_id() function in my Model and it's an invalid id. Should i use show_error() in my Model or return FALSE and use show_error() in my Controller ?

Thanks in advance!
#2

[eluser]sikkle[/eluser]
I think in the MCV approach and in fact to always control what you do,

get TRUE or FALSE from your model, and do whatever you want after in the controller to control it.
#3

[eluser]the real rlee[/eluser]
Yeah that's what i was initially thinking. But then the Controller gets crowded with conditional checks. And if the error is related to the data acquisition almost makes sense to prompt in the model...

Code:
function get_user_id(){
   if (!$query = $this->db->query('SELECT * FROM user WHERE user_id = '.(int)$user_id)) {
       show_error('Invalid ID');
   }
   return $query->row_array();
}
#4

[eluser]sikkle[/eluser]
Model

Code:
function getLastConnection($user_id)
    {
        
        
        $query = $this->db
        
                ->select($this->client_connection)
                ->from($this->_table)
                 ->where(array($this->client_id=>$user_id))
                 ->limit(1)
                 ->get();
        
              
           if($query->num_rows())
        {
            
             $userquery = $query->row_array();
             return $userquery;

        }
        
        else
        {
                    
            return false;
            
        }
        
        
    }


Controller

Code:
if (!$this->wrawra->getLastConnection()) {redirect('welcome', 'location') OR show message or WHATEVER.;}

Maybe i'm wrong you tell me Smile
#5

[eluser]Michael Wales[/eluser]
I always get TRUE or FALSE from my model (unless I am requesting a record, or series of records, then I return an object to represent that or FALSE).

I then use my Controller to show_error() if need be.
#6

[eluser]the real rlee[/eluser]
Hmm yeah i get what your both saying. I was just trying to avoid testing the result everytime I called my model function within my Controller :/. MVC is great but knowing where to put things is difficult at the best of times lol
#7

[eluser]Luipaard[/eluser]
How about exceptions, if you're using PHP 5 that is.

You throw the error in the model class, and you can choose to handle it in the controller, or just let PHP raise it.

Model
Code:
function get_user_id(){
   if (!$query = $this->db->query('SELECT * FROM user WHERE user_id = '.(int)$user_id)) {
       throw new Exception('Invalid ID');
   }
   return $query->row_array();
}

Controller
Code:
function index() {
//load the model and stuff here
  try {
  //call the function above here
  } catch (Exception $e) {
      $data['message'] = $e;
      $this->load->view('exception', $data);
  }
}


Exceptions
#8

[eluser]the real rlee[/eluser]
Hmmm yeah i like your thinking Luipaard, unfortunately the app im working on is in PHP4 Sad.. Really should move to PHP5




Theme © iAndrew 2016 - Forum software by © MyBB