Welcome Guest, Not a member yet? Register   Sign In
Multiple applications accessing the same models
#1

[eluser]jleequeen[/eluser]
Hello Everyone,

Is there a preferred way to have multiple CI applications using the same model? For instance, if you have one database that multiple applications need to access. I'd appreciate any feedback.

Thanks,

Lee
#2

[eluser]tonanbarbarian[/eluser]
There is no preferred way to do this
There are a couple ways you can do it though

1. put the same model into all of your apps
2. use something like matchbox to allow multiple apps to run together and store the model in the root models folder rather than the app model folder.
#3

[eluser]gon[/eluser]
You can use Phing to deploy common files into your project folder.
This way you can have a single copy of any file that you'll be sharing between projects.
#4

[eluser]xwero[/eluser]
Because you can autoload models now a models directory in the system directory would be a welcome addition for this sort of thing.

For the time being you can extend the loader library to work with a models directory in the system directory. I've added the possibility to extend the model like you would do for the libraries with the prefix in the config.
Code:
class MY_Loader extends CI_Loader
{

        function MY_Loader()
        {
            parent::CI_Loader();
        }

        function model($model, $name = '', $db_conn = FALSE)
    {        
        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);

        $system_model = FALSE;
        if (file_exists(BASEPATH.'models/'.$path.$model.EXT))
        {
            $basepath_model = TRUE;
        }
        
            $application_model = FALSE;
        if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
        {
            show_error('Unable to locate the model you have specified: '.$model);
        }
            else
            {
                $application_model = $path.$model;
            }

            if($application_model === FALSE && file_exists(BASEPATH.'models/'.$path.config_item('subclass_prefix').$model.EXT))
            {
                $application_model = $path.config_item('subclass_prefix').$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($system_model === TRUE)
            {
                require_once(BASEPATH.'models/'.$path.$model.EXT);
            }

            if(is_string($application_model))
            {
                 require_once(APPPATH.'models/'.$application_model.EXT);
            }

        $model = ucfirst($model);
                
        $CI->$name = new $model();
        $CI->$name->_assign_libraries();
        
        $this->_ci_models[] = $name;    
    }
}
The code is untested.




Theme © iAndrew 2016 - Forum software by © MyBB