EllisLab text mark
Advanced Search
     
Should Models output application errors?
Posted: 30 August 2007 11:47 PM   [ Ignore ]
Avatar
Joined: 2006-09-24
49 posts

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!

 
Posted: 31 August 2007 12:04 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-05-21
340 posts

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.

 Signature 

-> None official irc channel [ irc.freenode.net #codeigniter ]

 
Posted: 31 August 2007 12:18 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2006-09-24
49 posts

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…

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();
 
Posted: 31 August 2007 01:34 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2007-05-21
340 posts

Model

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

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

Maybe i’m wrong you tell me smile

 Signature 

-> None official irc channel [ irc.freenode.net #codeigniter ]

 
Posted: 31 August 2007 01:44 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-07-30
2144 posts

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.

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

 
Posted: 31 August 2007 01:56 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2006-09-24
49 posts

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

 
Posted: 31 August 2007 02:08 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-27
5 posts

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

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

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

 Signature 

Blood Inside.

 
Posted: 31 August 2007 03:50 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2006-09-24
49 posts

Hmmm yeah i like your thinking Luipaard, unfortunately the app im working on is in PHP4 :(.. Really should move to PHP5