php - Error "reporting" in a model -
i'm thinking of returning errors ocurred inside model, 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 empty!'); $this->set_error('dev','// db error in full'); return false; } }
this way can treat them on controller:
class data extends ci_controller { public function index(){ $this->load->model('data'); $data = $this->data->data(); if(!$data) // send $this->data->get_errors() user , logs else // send $data view } }
is idea? potential drawbacks, , there better way go treating db operations/data validation errors?
new "evidence": http://www.firehed.net/mvc-model-data-validation
crud model. better have model throw exception controller. controller catches , whatever needs done.
model:
class data_model extends ci_model { public function getdata($id){ if( /* worthy of error */) { throw new exeption('your db seems 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() user , logs } // send $data view } }
Comments
Post a Comment