TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Multi-configures for Database
  • Recently i deploy redmine on my host that it is writed by ROR framework. i found it's database configure is cool. in different environment with different configure. then i copy this think to kohana.

    1. Extending Kohana Database library (*libraries/MY_Database.php*)

    class Database extends Database_Core {

    /**
    * Multiway profile of Database configures
    */
    public function __construct()
    {
    $config = array();

    // custom environment key to databse config.
    $environment = Kohana::config('database.environment');
    // available: development, test, pro

    if ( !is_array($environment) && !empty($environment) )
    {
    $config = Kohana::config('database.'.$environment);
    }

    parent:GDN__construct($config);
    }
    }


    2. Modify *config/databse.php*

    // Sets the default databse config to "development"
    $config['environment'] = 'development';

    // original Kohana database config
    $config['default'] = array
    (
    'benchmark' => TRUE,
    'persistent' => FALSE,
    'connection' => array
    (
    'type' => 'mysql',
    'user' => 'dbuser',
    'pass' => '[email protected]',
    'host' => 'localhost',
    'port' => FALSE,
    'socket' => FALSE,
    'database' => 'database_name'
    ),
    'character_set' => 'utf8',
    'table_prefix' => '',
    'object' => TRUE,
    'cache' => FALSE,
    'escape' => TRUE
    );

    // Development
    $config['development'] = array
    (
    'benchmark' => TRUE,
    'persistent' => FALSE,
    'connection' => array
    (
    'type' => 'mysql',
    'user' => 'dbuser',
    'pass' => '[email protected]',
    'host' => 'localhost',
    'port' => FALSE,
    'socket' => FALSE,
    'database' => 'database_name'
    ),
    'character_set' => 'utf8',
    'table_prefix' => '',
    'object' => TRUE,
    'cache' => FALSE,
    'escape' => TRUE
    );

    // Test online
    $config['test'] = array
    (
    'benchmark' => TRUE,
    'persistent' => FALSE,
    'connection' => array
    (
    'type' => 'mysql',
    'user' => 'dbuser',
    'pass' => '[email protected]',
    'host' => 'localhost',
    'port' => FALSE,
    'socket' => FALSE,
    'database' => 'database_name'
    ),
    'character_set' => 'utf8',
    'table_prefix' => '',
    'object' => TRUE,
    'cache' => FALSE,
    'escape' => TRUE
    );

    // Production online
    $config['production'] = array
    (
    'benchmark' => TRUE,
    'persistent' => FALSE,
    'connection' => array
    (
    'type' => 'mysql',
    'user' => 'dbuser',
    'pass' => '[email protected]',
    'host' => 'localhost',
    'port' => FALSE,
    'socket' => FALSE,
    'database' => 'database_name'
    ),
    'character_set' => 'utf8',
    'table_prefix' => '',
    'object' => TRUE,
    'cache' => FALSE,
    'escape' => TRUE
    );


    $config['default'] array was not removed in database.php, because it will avoid unkown error.

    i also submit a issure on kohana dev, i hope this feature in include kohana system: http://dev.kohanaphp.com/issues/1915

    Source:
    libraries/MY_Database.php:http://code.google.com/p/kohana-fans-cn/source/browse/trunk/libraries/MY_Database.php
    config/database.php:http://code.google.com/p/kohana-fans-cn/source/browse/trunk/config/database.php
  • @icyleaf, with your modification, it's hard to support multi-databases for a certain environment.
    another way I get to make it works like what your have done and do not need to extend the Database_Core.

    // config/databse.php
    $config['environment'] = 'development';
    switch($config['environment'])
    {
    case 'production':
    $config['default'] = array(...);
    break;
    case 'development':
    $config['default'] = array(...);
    break;
    case 'test':
    $config['default'] = array(...);
    break;
    default:
    $config['default'] = array(...);
    break;
    }
  • @Yahasana: Great work! This is simple way! my way was detoured.
  • I came up with some similar logic in the database config file in this thread.

    But isn't logic in a config file dirty? I think setting the "default" database in a parent model constructor (so that Kohana won't blindly load the default config) is a better approach:

    $config = (IS_PRODUCTION) ? Kohana::config('database')->production : Kohana::config('database')->development;
    Database::instance('default', $config);


    Edit: Probably best to do this in the bootstrap file so there is no chance of any queries being run with wrong default database.

    Update:
    Another, probably better solution:

    $environment = Kohana::$environment;
    Kohana::config('database')->default = Kohana::config('database')->$environment;

    This config overwrite works because the Kohana configuration array is static.
  • It is sufficient to do all in the config file if you like me prefer that.

    <?php defined('SYSPATH') or die('No direct script access.');
     
    $config = array
    (
        Kohana::PRODUCTION => array
        (
        ...
        ), 
        Kohana::DEVELOPMENT => array
        (
        ...
        ), 
    );
    
    $config['default'] = $config[Kohana::$environment];
    
    return $config;
    
  • With the improvements to config merging in 3.2 its actually much better to do

    Kohana::$config->attach(new Config_File);
    Kohana::$config->attach(new Config_File('config/'.$environment));
    

    And then just place extra config files in eg config/production/database.php overriding only the keys you need to be different - usually just host, user, password etc.

    Much cleaner and easier to maintain, plus helps with the fact that you'll almost always want to keep bits of production config out of source control.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion