Welcome Guest, Not a member yet? Register   Sign In
Give an autoloaded model an alias.
#1

[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.
#2

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

[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);
            }
            
        }
        
    }
    
}
#4

[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.
#5

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

[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...




Theme © iAndrew 2016 - Forum software by © MyBB