Reputation: 4267
When I manually load models in codeigniter
I can specify an alias like so:
$this->load->model("user_model","user"); //user is an alias to user_model
$this->user->getProfile(); //use the alias to refer to the actual model
Some of these models are being extensively used in my application and so I decided to autoload them using autoload.php
. I know I can load them so:
$autoload['model'] = array("user_model","another_model");
However they are referenced all over with their aliases. I want to load them with existing alias name so that the current code is not disturbed.
I guess I can have some code like this in an autoloaded helper maybe:
$ci= &get_instance();
$ci->user = $ci->user_model;
But what I wanted to check is, can I load model with alias name while autoloading?
Upvotes: 2
Views: 7945
Reputation: 72560
For CodeIgniter 2.x this is not possible in autoloading, but you can do it by extending the default controller. Create a file MY_Controller.php
in the application/core
directory, with this code:
<?php
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Example_model', 'alias');
}
}
Of course, replace Example_model
and alias
with your appropriate model and desired alias.
Then change your controllers to extend MY_Controller
instead of CI_Controller
. Now you can use $this->alias->whatever()
in any Controller.
Upvotes: 1
Reputation: 13728
yes you can create same alias in in autoload pass as an array try but not possiable with only alias you can create same alias as auto loading time.
$autoload['model'] = array(array('users_model', 'users'), array('an_model', 'an'), 'other_model');
or try
$autoload['model'] = array(array('users_model', 'users', FALSE));
For more :- https://github.com/EllisLab/CodeIgniter/issues/2117
http://ellislab.com/forums/viewthread/110977/#560168
Upvotes: 3