7

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?

1
  • 1
    Yes you can and it will work
    – tomexsans
    Commented Feb 18, 2013 at 23:41

4 Answers 4

14

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.

1
  • Similarly in MY_Loader: function get_ci_models() { return $this->_ci_models; } # get_ci_models
    – kklepper
    Commented May 18, 2021 at 16:12
8

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..

4

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.

1
  • 1
    this 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
    Commented Feb 19, 2013 at 1:33
4

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 :)

2
  • 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!
    – Someone
    Commented Apr 25, 2015 at 13:15
  • AFAICS this is not possible as the parent class already checks if a class has been loaded, so it will not load it twice: if (in_array($name, $this->_ci_models, TRUE)) { return $this; }
    – kklepper
    Commented May 18, 2021 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.