PHP and Codeigniter - How do you check if a model exists and/or not throw an error?
You can see if the file exists in the models folder.
$model = 'my_model';if(file_exists(APPPATH."models/$model.php")){ $this->load->model($model); $this->my_model->my_fcn($prams);}else{ // model doesn't exist}
Maybe this helper function will help you to check if a model is loaded or not.
function is_model_loaded($model){ $ci =& get_instance(); $load_arr = (array) $ci->load; $mod_arr = array(); foreach ($load_arr as $key => $value) { if (substr(trim($key), 2, 50) == "_ci_models") $mod_arr = $value; } //print_r($mod_arr);die; if (in_array($model, $mod_arr)) return TRUE; return FALSE;}
Don't foget that your application may use pakages. This helper function look through all models (even in packages included in your CI app).
if ( ! function_exists('model_exists')){ function model_exists($name){ $CI = &get_instance(); foreach($CI->config->_config_paths as $config_path)if(file_exists(FCPATH . $config_path . 'models/' . $name . '.php'))return true; return false; }}
Cheers