0

I'm thinking of returning errors that ocurred inside a model, the following way:

class Data extends CI_Model {  
     private $errors_list

     private function Set_error($control,$error_string){
          $this->errors_list[][$control] = $error_string; 
     } 

     public function Get_errors($control){
          // logic
          return $errors_array;
     }

     public function Data(){
          // error happens
          $this->set_error('User','Your db seems to be empty!');
          $this->set_error('Dev','// DB error in full');
          return false;
     }
}

This way I can treat them on the controller:

class Data extends CI_Controller {
     public function index(){
          $this->load->model('Data');
          $data = $this->data->data();
          if(!$data)
          // send $this->data->get_errors() to user and logs
          else
              // send $data to view
     }
}

Is that a good idea? What are the potential drawbacks, and is there a better way to go about treating db operations/data validation errors?

New "evidence": http://www.firehed.net/mvc-model-data-validation

localhost
  • 450
  • 2
  • 6
  • 24

2 Answers2

0

CRUD is for the model. What would be better is to have the model throw an exception back to the controller. The controller catches it and does whatever needs to be done.

Model:

class Data_model extends CI_Model {  
    public function getData($id){
        if( /* something worthy of an error */) {
            throw new Exeption('Your db seems to be empty!');
        }    
    }
}

Controller:

class Data extends CI_Controller {
    public function index(){
        $this->load->model('Data_model','', 'data');
        try {
            $data = $this->data->getData();
        } catch (Exception $e) {
            // send $e->getMessage() to user and logs
        }
        // send $data to view
    }
}
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • I saw this here when searching, but it seems kinda messy. Even the mothod I described above isnt exactly what I want. I don't exactly want to "throw" things around, hahaha. – localhost Mar 11 '12 at 02:28
  • Not only is it not messy, it's the proper way to do it. – AlienWebguy Mar 11 '12 at 02:55
  • So some dude on some random blog posts a personal preference in 2009 and you unaccept? – AlienWebguy Mar 12 '12 at 20:28
  • Aren't we all "some dudes"? I just wanted to continue the discussion. And aparently the idea of "fat models" comes from the Rails community: http://bitfluxx.com/2008/01/23/cakephp-best-practices-fat-models-and-skinny-controllers.html and some other answers here: http://stackoverflow.com/questions/467113/fat-models-skinny-controllers-and-the-mvc-design-pattern – localhost Mar 12 '12 at 22:28
  • But I will flag this answer as accepted again, as there might not be much more to add to the subject. – localhost Mar 12 '12 at 22:29
0

@localhost2 Your way makes sense. Actually model validation error should belong to model Controller should not save any error list except flash messages( ie successful ...). So save the errors in model and send the model to view and in the view side you can extract the errors.

Elbek
  • 3,434
  • 6
  • 37
  • 49