Welcome Guest, Not a member yet? Register   Sign In
Help with database load design
#2

(This post was last modified: 08-09-2017, 06:42 PM by jarmen_kell. Edit Reason: CI's forum messes up with php code syntax highlight )

I've been thru the same case just like you, years ago.
here's what i did


create a "core" model, let's just call it "Coremodel" 
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Coremodel extends CI_Model {
    
    public static 
$USR_DB;
    
    public function 
__construct()
    {
        
parent::__construct();
        
        
self::$USR_DB['user_1']     = $this->load->database('user_1',true);
        
self::$USR_DB['user_2']     = $this->load->database('user_2',true);
        
self::$USR_DB['user_3']     = $this->load->database('user_3',true);
        
// and so'on
    
}



this model will act as a core parent for all models that you'll create later on.notice that i put a public-static variable inside this 'core' model.


later on, when you try creating models for each of your user's custom db connection,
just extend from our "Coremodel" 
PHP Code:
class Model_user1 extends Coremodel {
    
    private 
$_DB;
    
    function 
__construct() { 
        
parent::__construct();
        
$this->_DB     parent::$USR_DB['user_1'];
    }
    
    public function 
get($id)
    {
        return 
$this->_DB->get_where("table_name", ['id'=>$id]);
    } 

notice that i put a private var "_DB" and then retrieve our db connection instance from "Coremodel",then put it in private var "_DB". 


lastly, make sure that our "Coremodel" is autoloaded from autoload config.
PHP Code:
$autoload['model'] = array('coremodel'); 


now you have all of your model's connected accordingly,
without haveing to create a new connection for each models.

hope this make sense.
Reply


Messages In This Thread
Help with database load design - by NickBrown1968 - 08-09-2017, 07:38 AM
RE: Help with database load design - by jarmen_kell - 08-09-2017, 06:39 PM
RE: Help with database load design - by Diederik - 08-11-2017, 02:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB