Welcome Guest, Not a member yet? Register   Sign In
Extended Loader Class for Changing Model Object Name
#1

[eluser]Unknown[/eluser]
I wanted to be able to change the object name of a Model that was being loaded in an array. Specifically, I was autoloading multiple models. When loading models in an array I couldn't change the name of the object that the model was loaded as, so I extended the Loader class library. Hope this helps someone.
Usage:
Code:
$this->load->model(array('model1'=>'model1_name', 'model2'));

The only changes that needed to be made were:
Code:
if (is_array($model))
{
    foreach($model as $babe=>$name)
    {
        //  Does the babe have a name? If not just call her babe.
        if($babe == '') {
            $babe = $name;
        }
        $this->model($babe, $name);    
    }
    return;
}

To implement, create a file called MY_Loader.php in /system/application/libraries.
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Loader extends CI_loader {
    function model($model, $name = '', $db_conn = FALSE)
    {        
        if (is_array($model))
        {
            foreach($model as $babe=>$name)
            {
                //  Does the babe have a name? If not just call her babe.
                if($babe == '') {
                    $babe = $name;
                }
                $this->model($babe, $name);    
            }
            return;
        }

        if ($model == '')
        {
            return;
        }
    
        // Is the model in a sub-folder? If so, parse out the filename and path.
        if (strpos($model, '/') === FALSE)
        {
            $path = '';
        }
        else
        {
            $x = explode('/', $model);
            $model = end($x);            
            unset($x[count($x)-1]);
            $path = implode('/', $x).'/';
        }
    
        if ($name == '')
        {
            $name = $model;
        }
        
        if (in_array($name, $this->_ci_models, TRUE))
        {
            return;
        }
        
        $CI =& get_instance();
        if (isset($CI->$name))
        {
            show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
        }
    
        $model = strtolower($model);
        
        if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
        {
            show_error('Unable to locate the model you have specified: '.$model);
        }
                
        if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
        {
            if ($db_conn === TRUE)
                $db_conn = '';
        
            $CI->load->database($db_conn, FALSE, TRUE);
        }
    
        if ( ! class_exists('Model'))
        {
            load_class('Model', FALSE);
        }

        require_once(APPPATH.'models/'.$path.$model.EXT);

        $model = ucfirst($model);
                
        $CI->$name = new $model();
        $CI->$name->_assign_libraries();
        
        $this->_ci_models[] = $name;    
    }

}
?>




Theme © iAndrew 2016 - Forum software by © MyBB