Welcome Guest, Not a member yet? Register   Sign In
Autoload model with a different object name
#1

[eluser]RJ[/eluser]
Gm,

Is it possible to auto a model and assign it a different object name?

In a controller I would do this:
Code:
$this->load->model('Model_name', 'fubar');

In autoload the second param would be another model name.
Code:
$autoload['model'] = array('Common');

Thank you.
#2

[eluser]TheFuzzy0ne[/eluser]
That's a very good question, and unfortunately there's no way to do that. As far as I can see, this leaves two options.

Option 1 - You load a helper file or something similar, which will reassign the autoloaded model to a name of your liking. Perhaps something like this:

Code:
function reassign_model_names()
{
    $CI =& get_instance();
    $db = $CI->original_model;
    unset($CI->original_model);
    $CI->new_model_name =& $db;
}

...But that's fugly, and clunky.

Option 2 - Override the Loader library with your own method:

./system/application/libraries/MY_Loader.php

# CODE MOVED TO http://ellislab.com/forums/viewthread/110977/#560058

The code above is untested, but should allow you to use an array in your autoloader:

Code:
$autoload['libraries'] = array('database', array('email', NULL, 'email2'), 'session');

The arguments in the array should be just as they would be if you were to call $this->load->library() directly:
Code:
array($library_name, $params, $object_name)

Hope this helps.
#3

[eluser]TheFuzzy0ne[/eluser]
Ah nuts. I got lost there, and this will only allow you to load libraries in that fashion, not models... Sorry. I'll go back to the drawing board soon.
#4

[eluser]wiredesignz[/eluser]
Try this method in a CI_Loader class extension
Code:
/* autoload models */
        if (isset($autoload['model']))
        {
            foreach ($autoload['model'] as $model => $alias)
            {
                (is_numeric($model)) ? $this->model($alias) : $this->model($model, $alias);
            }
        }
Courtesy Modular Extensions - HMVC
#5

[eluser]TheFuzzy0ne[/eluser]
OK, I started over, and I noticed that the model and library loader methods worked a lot more differently to each other than I'd thought, so I decided to override both the model() and library() methods in the loader class. I figured that you might as well allow the same behaviour with libraries too, although you are free to delete that method if you wish. I also think I might have spotted a potential bug in the library loader, but I need to look into that a bit further.

./system/application/libraries/MY_Loader.php
Code:
<?php

class MY_Loader extends CI_Loader {
        
    function library($library = '', $params = NULL, $object_name = NULL)
    {
        if ($library == '')
        {
            return FALSE;
        }

        if ( ! is_null($params) AND ! is_array($params))
        {
            $params = NULL;
        }

        if (is_array($library))
        {
            foreach ($library as $class)
            {
# START EDIT
                if (is_array($class)
                {
                    list($_class, $_params, $_object_name) = $class;
                    $this->_ci_load_class($_class, $params, $_object_name);
                }
                else
                {
                    # Is this a bug? CI would try to use the same paramters and
                    # object names on all libraries in the array??
                    $this->_ci_load_class($class, $params, $object_name);
                }
# END EDIT
            }
        }
        else
        {
            $this->_ci_load_class($library, $params, $object_name);
        }
        
        $this->_ci_assign_to_models();
    }
    
    function model($model, $name = '', $db_conn = FALSE)
    {        
        if (is_array($model))
        {
            foreach($model as $babe)
            {
# START EDIT
                if (is_array($babe))
                {
                    if (isset($babe[0]))
                    {
                        $model = $babe[0];
                        $name = (isset($babe[1])) ? $babe[1] : NULL;
                        $db_conn = (isset($babe[2])) ? $babe[2] : FALSE;
                        $this->model($model, $name, $db_conn);
                    }
                }
                else
                {
                    $this->model($babe);
                }
# END EDIT
            }
            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;    
    }
}
#6

[eluser]RJ[/eluser]
Close!

Line 37 needs ")"

and I get this error:
Quote:Message: Missing argument 1 for Dealer_model:Big Grinealer_model(), called in W:\www\s\system\application\libraries\FW_Loader.php on line 137 and defined

On this bit of code:
Code:
$CI->$name = new $model();

Off of this auto load:
Code:
$autoload['model'] = array(array('dealer_model', NULL, 'dealer'));
#7

[eluser]TheFuzzy0ne[/eluser]
You are using the syntax for loading a library. In your autoload.php file, to load a model you'd need to do something like this:

Code:
$autoload['model'] = array(array('dealer_model', 'dealer'));
#8

[eluser]RJ[/eluser]
This is the method in my main controller calling the deal model and passing $dealer from the uri. Not passing right for some reason

Code:
function device($dealer = '999')
    {
        $this->benchmark->mark('code_start');
        // start benchmarking

            $this->dealer($dealer);   // passes db info to dealer library for processing
            $this->dealer->dealer($dealer); // tried this as well

This is my dealer_model:
Code:
class Dealer_model extends Model {

    var $dealer    =    "";
    var $vars    =    "";
    
#--------------------------------------------------------------------------
# Controller
#--------------------------------------------------------------------------
    function Dealer_model($dealer)
    {
        // Call the Model constructor
        parent::Model;

        print_r($dealer);
        // submit log entry
        log_message('debug', "Dealer Class Initialized");
        // on screen php error display
        ini_set('display_errors', 1);
        // use dealer var to grab info from db, set $vars
        $this->vars = $this->grab_vars($dealer);
        // add dealer info to session
        $this->session->set_userdata('dealer',$this->vars);        
        // submit log entry
        log_message('debug', "Dealer routines successfully run");
    }
#9

[eluser]TheFuzzy0ne[/eluser]
I've just borked my Aptana install, so I'm reinstalling it now. I'll do some debugging shortly.
#10

[eluser]RJ[/eluser]
Ok. So I can access the model with this:
Code:
$this->dealer->dealer_model($dealer);

Not entirely sure why I need to name the constructor for my dealer_model, but it doesnt work without that.

I can't get my controller to send the URI data to the model; had a similar issue with a library, fixed it, but i decided to use a model rather than a library, back at the same problem.

Controller:
Code:
function Shop()
    {
            parent::Controller();    
            $this->output->enable_profiler(TRUE);    // debug onscreen
            ini_set('display_errors', 1);            // on screen php error display
    }

/*
#--------------------------------------------------------------------------
# METHOD: Device Sub-Controller
#--------------------------------------------------------------------------
|
| http://findwhere.com/s/shop/device
| this is the Method that runs the above URL
*/
    function device($dealer = '999')
    {
        $this->benchmark->mark('code_start');
        // start benchmarking

            $this->dealer->dealer_model($dealer);        // passes db info to dealer library for processing

Dealer_model:
Code:
class Dealer_model extends Model {

    var $dealer    =    "";
    var $vars    =    "";
    
#--------------------------------------------------------------------------
# Controller
#--------------------------------------------------------------------------
    function Dealer_model($dealer)
    {
        // Call the Model constructor
        parent::Model();

        print_r($dealer);
        // submit log entry
        log_message('debug', "Dealer Class Initialized");
        // on screen php error display
        ini_set('display_errors', 1);
        // use dealer var to grab info from db, set $vars
        $this->vars = $this->grab_vars($dealer);
        // add dealer info to session
        $this->session->set_userdata('dealer',$this->vars);        
        // submit log entry
        log_message('debug', "Dealer routines successfully run");
    }


PHP ERRORS:
Code:
list($model, $name, $db_conn) = $babe;
Error:
Quote:Severity: Notice --> Undefined offset: 2 W:\www\s\system\application\libraries\FW_Loader.php 67
there are more, but those should be solved once the passing from controller to model is worked out.




Theme © iAndrew 2016 - Forum software by © MyBB