Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoloader should support loading a model under a different name #2117

Closed
t3nsor opened this issue Jan 4, 2013 · 12 comments
Closed

Autoloader should support loading a model under a different name #2117

t3nsor opened this issue Jan 4, 2013 · 12 comments

Comments

@t3nsor
Copy link

t3nsor commented Jan 4, 2013

CI_Loader::model() supports an optional second argument for renaming the model (cf. http://ellislab.com/codeigniter/user-guide/general/models.html#loading ). It should also be possible to provide a name when the model is autoloaded from $autoload['model'], e.g.,

$autoload['model'] = array(array('users_model', 'users'), array('comments_model', 'comments'), 'auth_model');

to load Users_model as "users", Comments_model as "comments", and Auth_model as "auth_model" (no optional name specified).

@sv3tli0
Copy link
Contributor

sv3tli0 commented Jan 4, 2013

$autoload['model'] = array(array('users_model', 'users'), array('comments_model', 'comments'), 'auth_model');

Has to be ..

$autoload['model'] = array('users_model'=> 'users', 'comments_model'=>'comments', 'auth_model');

@t3nsor
Copy link
Author

t3nsor commented Jan 4, 2013

Aha, silly me. Of course, that is simpler and nicer. Forgive me, it's 6 AM where I am.

@dwightwatson
Copy link

What about loading multiple models in the controller in such a fashion?

$this->load->model('users_model', 'users');
$this->load->model('comments_model', 'comments');
$this->load->model('auth_model');

To be like so:

$this->load->model(array('users_model' => 'users', 'comments_model' => 'comments', 'auth_model'));

@TheDigitalOrchard
Copy link

@dwightwatson The original poster is specifically talking about autoloading models, not doing so manually in the controller.

@dwightwatson
Copy link

@TheDigitalOrchard I am aware, but as far as the feature suggestion goes I think that mechanism of loading models with a custom name is needed by the loader outside of autoload.php as well as within.

@AkenRoberts
Copy link
Contributor

This should be relatively easy to do -- who's up for the challenge?

@dwightwatson
Copy link

I'll have a crack, but I'm not sure if CodeIgniter has a precedence for working out whether an array is sequential or associative.

$this->load->model(array('users_model', 'comments_model', 'auth_model'));

$this->load->model(array('users_model' => 'users', 'comments_model' => 'comments', 'auth_model'));

Does anyone have any tips on this?

@AkenRoberts
Copy link
Contributor

I would use another level of arrays.

$this->load->model(array(
    array('users_model' => 'users'),
    array('cart_model' => 'cart'),
    'sandbox',
    'monkey'
));

The individual arrays could then use key => value, or maybe two array items that can be list()ed apart.

@dwightwatson
Copy link

That makes perfect sense, I've now made a pull request to the develop branch.

#2150

@narfbg
Copy link
Contributor

narfbg commented Jan 12, 2013

There you go.

@narfbg narfbg closed this as completed Jan 12, 2013
nonchip pushed a commit to nonchip/CodeIgniter that referenced this issue Jun 29, 2013
@fdrobidoux
Copy link

I'm surprised this didn't get commited yet...

@Razican
Copy link
Contributor

Razican commented Nov 12, 2014

It did: b07f700

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants