Samik Chattopadhyay
Samik Chattopadhyay

Reputation: 1860

Autoloading model on demand in codeigniter

Can I somehow omit the first line? i.e. $this->load->model('Model_name'); and autoload it when necessary?

To load a model you will use the following function:

$this->load->model('Model_name');

Once loaded, you will access your model functions using an object with the same name as your class:

$this->Model_name->function(); 

Upvotes: 0

Views: 419

Answers (2)

user1673153
user1673153

Reputation: 1

You don't understand the logic .

$this->load->model('Model_name') means you load it when it necessary.

or

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.

Upvotes: 0

Laurence
Laurence

Reputation: 60058

Lazy loading is not supported in Codeiginiter 2.1

There are a couple of options available to you

  1. Use the 'autoload' in the config. This will always make the model available throughout the application. See here for more info

  2. Load the model in the __construct() of your controller (if it is specific to that controller)

Upvotes: 1

Related Questions