4
<?php
class Home extends CI_Controller
{
    public function __construct()
    {
        // load libraries //
        $this->load->library('session');
        $this->load->library('database');
        $this->load->library('captcha');

        // alternative
        $this->load->library(array('session', 'database', 'captcha'));

        // load models //
        $this->load->model('menu_model', 'mmodel');
        $this->load->model('user_model', 'umodel');
        $this->load->model('admin_model', 'amodel');

        // alternative
        $this->load->model(array(?));
    }
}
?>

How can i load all models in array? is it possible?

4 Answers 4

5

For models, you can do this:

$models = array(
    'menu_model' => 'mmodel',
    'user_model' => 'umodel',
    'admin_model' => 'amodel',
);

foreach ($models as $file => $object_name)
{
    $this->load->model($file, $object_name);
}

But as mentioned, you can create file application/core/MY_Loader.php and write your own method for loading models. I think this might work (not tested):

class MY_Loader extends CI_Loader {

    function model($model, $name = '', $db_conn = FALSE)
    {
        if (is_array($model))
        {
            foreach ($model as $file => $object_name)
            {
                // Linear array was passed, be backwards compatible.
                // CI already allows loading models as arrays, but does
                // not accept the model name param, just the file name
                if ( ! is_string($file)) 
                {
                    $file = $object_name;
                    $object_name = NULL;
                }
                parent::model($file, $object_name);
            }
            return;
        }

        // Call the default method otherwise
        parent::model($model, $name, $db_conn);
    }
}

Usage with our variable from above:

$this->load->model($models);

You could also allow a separate DB connection to be passed in an array, but then you'd need to have a multidimensional array, and not the simple one we used. It's not too often you'll need to do that anyways.

Sign up to request clarification or add additional context in comments.

6 Comments

I would combine the two and make a models() method which simply iterates the array and calls model() on each.
Why make a new method? This doesn't affect the existing one in any way. Personally, I would just not use an array to load models in the first place, or just use the loop. Actually, just passing an array of file names (which can already be done) is what I would prefer, I don't like to use an alias object name.
They use $this->model($babe) as the variable name in the system file.. Ugh, so lame! :D
CI uses aliases in their core already. For example, helpers() is an alias of helper() which expects an array argument.
I'm +1'ing your post for cranking out the details ;)
|
1

I don't have any idea about the CodeIgniter 2.x but in CodeIgniter 3.x, this will also works :

$models = array(
   'menu_model' => 'mmodel',
   'user_model' => 'umodel',
   'admin_model' => 'amodel',
);
$this->load->model($models);

Comments

0

Not natively, but you can easily extend Loader->model() to support that logic.

Comments

0

This work fine for me:

$this->load->model(array('menu_model'=>'menu','user_model'=>'user','admin_model'=>'admin'));

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.