Welcome Guest, Not a member yet? Register   Sign In
How to autoload model / library witha different object name
#1

[eluser]Lone[/eluser]
Just curious if anyone could help me solve the following issue I am having. We need to autoload a model but am having double class naming issues with a controller that has the same class name.

Usually in a controller you can just go as below to access as $this->User

Code:
$this->load->model('user_model','User);
$this->User->function();

But when autoloading you are forced to have it name as $this->User_model when I want it as $this->User

Code:
//autoload.php
$autoload['model'] = array('user_model');
// controller
$this->User_model->function();

I know I can change the model filename and class name to 'User' but when its autoloaded in a contoller with the same classname as 'User' you get the double naming error.


Is there any easy way to achieve this without having to do the $this->load->model call in every controller construct - eg. back to autoloader?
#2

[eluser]Pascal Kriete[/eluser]
I always have my own base controller off of which I extend others, so I would do:
Code:
class BaseController extends Controller {
    var $User;
    function BaseController()
    {
        $this->User = $this->User_model;
        parent::Controller();
    }
}

Of course you only have that luxury if you extend the ci controller.
#3

[eluser]Lone[/eluser]
Nice idea there - but Im trying for no extra code in controllers to achieve this.
#4

[eluser]Pascal Kriete[/eluser]
I would love to see a solution to this, but I don't think it's possible. For it to be accessible 'globally', it needs to be either auto-loaded or a global variable. The only widely available variables that are not in the super-object scope are those in the ci_controller.
#5

[eluser]xwero[/eluser]
I think inparos solution is the only one for renaming a model that is autoloaded without hacking CI.
#6

[eluser]Lone[/eluser]
Well what this is ideally needing is the ability to do an associative array for the autoloader eg.

Code:
$autoload['model'] = array(
  'user_model' => 'user',
  'page_model' => 'page'
);

Maybe I should put as a feature request?

But for in the meantime I need to sort out a solution - I have been think a lil bit dirty by having a 'base model' and loading them like below..

Code:
$autoload['model'] = array('base_model');

class base_model {
  function base_model() {
    $CI =& get_instance();
    $CI->load->model('user_model','User');
    $CI->load->model('page_model','Page');
  }
]

Havent tried it but guessing will work in theory - just a lil dirty for my liking.
#7

[eluser]Lone[/eluser]
Ok I have made a feature request to cover this issue and resolve by allowing assoc arrays.

http://ellislab.com/forums/viewthread/72858/

Interestingly enough I have found that Matchbox already does this in a similar way Smile
#8

[eluser]wiredesignz[/eluser]
Code:
<shameless_plug>
    Modular Extensions HMVC has this feature also, plus many others.
</shameless_plug>




Theme © iAndrew 2016 - Forum software by © MyBB