Reputation: 161
I written code using Codeigniter, I want to increase for load time when I open my page for first time. Actually when I written code I load all the model in my contruct like this:
public function __construct(){
parent::__construct();
$this->load->model('Fa_promo');
$this->load->model('Fa_project');
$this->load->model('Fa_inbox');
$this->load->model('Fa_quotes');
$this->data['keywords']=$this->Configuration->get_keyword();
$this->data['about'] = $this->Fa_menu->get__content_from__slug('about');
$this->data['menu']=$this->Fa_menu->list_menu_level1();
$this->Configuration->update_hits();
}
My question, how to do the best way to load the model to increase performance between above or like this?
public function __construct(){
parent::__construct();
}
public function a(){
$this->load->model('Fa_promo');
$this->Fa_promo->load();
}
public function (){
$this->load->model('Fa_project');
$this->Fa_project->load();
}
Thanks for your help :)
Upvotes: 2
Views: 2676
Reputation: 4265
It will be faster to load them individually in each function as you can limit the number of calls made to load a model, but the difference is likely to be minimal
If you load them all in the constructor, then you can use them at any point in the controller from one load, which might be a little slower, but the benefit is they are loaded and ready. It could use more memory than needed for a single given function though.
If you want to save code for loading the models, you could always do the following in the constructor:
$this->load->model(['Fa_promo', 'Fa_project', 'Fa_inbox', 'Fa_quotes]);
This will load all of the models in one go. There's no real time benefit of this as passing an array causes the loader to call itself for each item, so it's the same as you currently have.
Upvotes: 3
Reputation: 7997
The codeigniter docs say: Your models will typically be loaded and called from within your controller methods.
The first approach has a great advantage: you can reuse it in all controller methods and don't have to type in each method over and over again.
concerning performance: I think it doesn't make any difference, the functions in the model might slow down things
Upvotes: 2