Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Model loader can load multiple models with alternate name #2150

Closed
wants to merge 1 commit into from

3 participants

@dwightwatson

This allows users to load multiple models at the same time while using alternate names for the object if they like.

$this->load->model(array('model_one', 'model_two'));

$this->load->model(array(array('model_one', 'foobar'), 'model_two'));
@narfbg narfbg closed this
@cryode

Is this tested to actually work with the autoloader, too? Because both were in question - autoload with an alias, as well as load through the Loader class. Just double checking.

@narfbg
Owner

I'd be surprised if it doesn't:

            // Autoload models
            if (isset($autoload['model']))
            {
                    $this->model($autoload['model']);
            }

Edit: Erm ... I was talking about my commit, this pull request is closed, not merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 12, 2013
  1. @dwightwatson

    Model loader can now load multiple models with alternate name

    dwightwatson authored
    Signed-off-by: Dwight Watson <dwightwatson@me.com>
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 3 deletions.
  1. +12 −3 system/core/Loader.php
  2. +7 −0 user_guide_src/source/general/models.rst
View
15 system/core/Loader.php
@@ -233,11 +233,20 @@ public function model($model, $name = '', $db_conn = FALSE)
}
elseif (is_array($model))
{
- foreach ($model as $class)
+ foreach ($model as $single_model)
{
- $this->model($class);
+ if (is_array($single_model))
+ {
+ list($class, $name) = $single_model;
+ $this->model($class, $name);
+ return;
+ }
+ else
+ {
+ $this->model($single_model);
+ return;
+ }
}
- return;
}
$path = '';
View
7 user_guide_src/source/general/models.rst
@@ -129,6 +129,13 @@ specify it via the second parameter of the loading method::
$this->foobar->method();
+You can also load multiple models at the same time, and choose to use
+a different object name if you like.
+
+ $this->load->model(array('model_name', 'another_model_name'));
+
+ $this->load->model(array(array('model_name', 'foobar'), 'another_model_name'));
+
Here is an example of a controller, that loads a model, then serves a
view::
Something went wrong with that request. Please try again.