Are there native codeigniter functions I can use to tell if a certain model has already been loaded? Can php's class_exists()
be used to tell if a model has already been loaded?
-
1Yes you can and it will work – tomexsans Feb 18 '13 at 23:41
I would be tempted to extend the CI_Loader
core class. (See extending Core Class)
class MY_Loader extends CI_Loader {
function __construct()
{
parent::__construct();
}
/**
* Returns true if the model with the given name is loaded; false otherwise.
*
* @param string name for the model
* @return bool
*/
public function is_model_loaded($name)
{
return in_array($name, $this->_ci_models, TRUE);
}
}
You would be checking for a given model with the following:
$this->load->is_model_loaded('foobar');
That strategy is already being used by the CI_Loader
class.
This solution supports the model naming feature of CI, where models can have a different name than the model class itself. The class_exists
solution wouldn't support that feature, but should work fine if you aren't renaming models.
Note: If you changed your subclass_prefix
configuration, it might not be MY_
anymore.
The simplest solution is to use PHP function class_exists
http://php.net/manual/en/function.class-exists.php
For example. if you want to check if Post_model has been defined or not.
$this->load->model('post_model');
/*
a lot of code
*/
if ( class_exists("Post_model") ) {
// yes
}
else {
// no
}
The simplest is the best..
Edited:
You can use the log_message() function.
Put this in your model’s constructor (parent::Model())
log_message ("debug", "model is loaded");
don’t forget to set the log config to debug mode in the config.php file
$config['log_threshold'] = 2;
And set the system/logs directory permission to writable (by default CI will create the log files here)
or set the logs directory in another dir
$config['log_path'] = 'another/directory/logs/';
CI will then create the log file in the directory. monitor the log files as you like. You can get the debug message to see if your model is already loaded or not in the log files.
-
1this will only let me know if the file exists, I wanted to know if the first model has been loaded maually in the controller or if has been automatically loaded via autoload – Zaki Aziz Feb 19 '13 at 1:33
Riffing off what Maxime Morin & Tomexsans have written, this is my solution:
<?php
class MY_Loader extends CI_Loader {
/**
* Model Loader
*
* Overwrites the default behaviour
*
* @param string the name of the class
* @param string name for the model
* @param bool database connection
* @return void
*/
function model ($model, $name = '', $db_conn = FALSE) {
if (is_array($model) || !class_exists($model)) {
parent::model($model, $name, $db_conn);
}
}
}
?>
This way, you don't ever need to (consciously) check whether a model's loaded or not :)
-
I ran into a situation where I was loading classes more than once and it was not throwing any error's but it was using up PHP memory limit and this fixed my issue without needing to modify any other code so thanks a lot! – Sam Apr 25 '15 at 13:15