CodeIgniter Forums
Give an autoloaded model an alias. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Give an autoloaded model an alias. (/showthread.php?tid=15649)



Give an autoloaded model an alias. - El Forum - 02-10-2009

[eluser]Mark Skilbeck[/eluser]
If we load a model like so
Code:
$this->load->model('My_model_with_a_long_name', 'My_model');

we give it an alias.

However, I cannot see a way to do this with autoloaded models.


Give an autoloaded model an alias. - El Forum - 02-10-2009

[eluser]moodh[/eluser]
I've also encountered this, a solution would be appriciated.


Give an autoloaded model an alias. - El Forum - 02-10-2009

[eluser]Silencez[/eluser]
Hi! I'm new with CI...

It must be a clean way to do this...

But, while you find it, you can try this:

(Just create a custom controller in application/library)

Code:
<?php

class MY_Controller extends Controller
{
    
    var $autoload_models = array
    (
    'My_alias'      => 'Model_with_long_name',
    'Another_alias' => 'Another_model_with_alias',
                       'Model_without_alias'
    );
    
    function MY_Controller()
    {
        parent::Controller();

        foreach($this->autoload_models as $alias => $model_name)
        {
            
            if(is_int($alias))
            {
                $this->load->model($model_name);
            }
            else
            {
                $this->load->model($model_name, $alias);
            }
            
        }
        
    }
    
}



Give an autoloaded model an alias. - El Forum - 02-11-2009

[eluser]xwero[/eluser]
The _ci_autoloader method in the loader class limits the parameters to be sure less mistakes are made.
Silencez method is clever but it still limits the model method because there is a third parameter.

using a hook, post_controller_constructor for example, is the best alternative to autloading.

A more permanent way to solve this would be to replace the _ci_autoloader call in the controller class with a pre_controller_constructor hook.


Give an autoloaded model an alias. - El Forum - 02-11-2009

[eluser]Mark Skilbeck[/eluser]
Hmm, I'm not going to mess around a bunch with the core. This should really be a feature, though.


Give an autoloaded model an alias. - El Forum - 02-11-2009

[eluser]Silencez[/eluser]
I insist I am new here.. But I think creating a hook or a custom controller is not messign with the core...