Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: how can i tell if a model is already loaded?

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?

like image 544
Zaki Aziz Avatar asked Feb 18 '13 23:02

Zaki Aziz


People also ask

How will you add or load a model in CodeIgniter?

Auto-loading Models If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload. php file and adding the model to the autoload array.

What is $this in CodeIgniter?

$this used to get/load things like helpers, libraries, database, global variables in your extended class. For example $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->load->library("pagination"); Follow this answer to receive notifications.

Can we load model in another model CodeIgniter?

In this case, you can either create a separate function to perform the same action or use the already available method in the Model. It is possible to reuse the method in the Model from another Model. Models are loaded in the Model as same as loaded in the Controller using $this->load->model() .

How do you call a model in view?

Create a Model to query through the database and return the data (as an array or object) Create a Controller to load and fetch the result from the Model (a method of the Model), and pass the returned data to the view. Create a View and use PHP loops to echo the result out, build the HTML.


4 Answers

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.

like image 72
Maxime Morin Avatar answered Oct 24 '22 20:10

Maxime Morin


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

like image 40
Terry Lin Avatar answered Oct 24 '22 20:10

Terry Lin


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.

like image 28
Tommy Adey Avatar answered Oct 24 '22 20:10

Tommy Adey


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

like image 34
Algy Taylor Avatar answered Oct 24 '22 20:10

Algy Taylor