Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cakephp - Using multiple database

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 367
    Comment on it

    Many a times we need use multiple database as per the domain or some other reason . We can easily achieve it with the following code in cakePhp.

    Write the following code in app->config->database.php

    class DATABASE_CONFIG {
    
        public $default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'root',
            'password' => '',
            'database' => 'firstDatabaseName',
            'prefix' => ''
        );
        public $first = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'root',
            'password' => '',
            'database' => 'firstDatabaseName',
            'prefix' => ''
        );
        public $second= array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'root',
            'password' => '',
            'database' => 'seondDatabaseName',
            'prefix' => ''
        );
    
       public function __construct() {
            $parsed = parse_url($_SERVER['HTTP_HOST']);
    
           $domain = explode('.', $parsed['path']);
            $subdomain = '';
            if ($domain[0] == 'www')
                $subdomain = $domain[1];
            else
                $subdomain = $domain[0];
            if ($subdomain == 'us') {
                // use site one database config
                $this->default = $this->second;
    
                // elseif site two
            }else if($subdomain == 'can'){
               $this->default = $this->first; 
            }
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: