Welcome Guest, Not a member yet? Register   Sign In
external model file loader....
#1

[eluser]HdotNET[/eluser]
instead of loading models from your models/ directory... add this to your libraries directory...

then you can load a CI model from another app if necessary...

eg

$this->load->model('/absolute/path/to/your/model.php');



Code:
<?php


/**
* extending the loader class... needs this in config file
* $config['subclass_prefix'] = 'MY_';
* see the docs http://ellislab.com/codeigniter/user-guide/general/core_classes.html
*
* @package    default
* @author    HdotNET
*/


class MY_Loader extends CI_Loader {

    function MY_Loader()
    {
        parent::CI_Loader();
    }
    
    /**
     * This function lets users load and instantiate models.
     * updated model function to allow calling of remote files.
     * @access    public
     * @param    string    the name of the class
     * @param    string    name for the model
     * @param    bool    database connection
     * @return    void
     **/
    function model($model, $name = '', $db_conn = FALSE){
    
        $model = str_replace(EXT,'',$model);

        if (is_array($model))
        {
            foreach($model as $babe)
            {
                $this->model($babe);    
            }
            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);
        
        $local_file = false;
        $remote_file = false;
        
        if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
        {
            $local_file = false;
        }
        else
        {
            $local_file = true;
        }

        if ( !$local_file && ! file_exists($path.$model.EXT))
        {
            $remote_file = true;
        }
        else
        {
            $remote_file = true;
        }
        
        if(!$local_file && !$remote_file)
        {
            show_error('Unable to locate the model you have specified:<br />'.$path.$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);
        }
        
        if($local_file)
        {
            require_once(APPPATH.'models/'.$path.$model.EXT);
        }
        else
        {
            require_once($path.$model.EXT);
        }
        
        $model = ucfirst($model);
                
        $CI->$name = new $model();
        $CI->$name->_assign_libraries();
        
        $this->_ci_models[] = $name;    
    }
}


?&gt;
#2

[eluser]Unknown[/eluser]
I have been trying to figure out a way to load models from different directory other than "application/models" directory. I want to load my model from "application/modules".

I have tried the instructions by extending CI_Loader by following instructions provided here
but it is giving me error "Unable to locate the specified class: Model.php" whereas my core "Model.php" is there under "system/core" folder.

I have also tried to load model using

$this->load->model (APPPATH.'modules/mymodel.php');
but no success.

Is there any way i could load models from outside the "application/modules" folder ? Please advise .Thanks




Theme © iAndrew 2016 - Forum software by © MyBB