0

SO I am trying to build out a master config file to use with deployment similar to that in EE2 for EE3. In the include, I have the following:

$config['database'] = array (
    'expressionengine' => array (
        'hostname' => 'localhost',
        'username' => 'local-UN',
        'password' => 'local-pw',
        'database' => 'local-db',
        'dbdriver' => 'mysqli',
        'dbprefix' => 'exp_',
        'pconnect' => FALSE
    ),
    'localhost' => array (
        'hostname' => 'localhost',
        'username' => 'local-UN',
        'password' => 'local-pw',
        'database' => 'local-db',
        'dbdriver' => 'mysqli',
        'dbprefix' => 'exp_',
        'pconnect' => FALSE
    ),
    'production' => array (
        'hostname' => 'external-server',
        'username' => 'ext-UN',
        'password' => 'ext-pw',
        'database' => 'ext-db',
        'dbdriver' => 'mysqli',
        'dbprefix' => 'exp_',
        'pconnect' => FALSE
    )
);

The reason I left the 'expressionengine' database in there is because of the call for it in DBConfig.php which I'll show in a moment. I know the databases are being populated because when I output print_r(ee()->config->config); I get the database array as this:

[cache_driver] => file
[database] => Array
    (
        [expressionengine] => Array
            (
                [hostname] => localhost
                [username] => local-UN
                [password] => local-pw
                [database] => local-db
                [dbdriver] => mysqli
                [dbprefix] => exp_
                [pconnect] => 
            )

        [localhost] => Array
            (
                [hostname] => localhost
                [username] => local-UN
                [password] => local-pw
                [database] => local-db
                [dbdriver] => mysqli
                [dbprefix] => exp_
                [pconnect] => 
            )

        [production] => Array
            (
                [hostname] => external-server
                [username] => ext-UN
                [password] => ext-pw
                [database] => ext-db
                [dbdriver] => mysqli
                [dbprefix] => exp_
                [pconnect] => 
            )

    )

[db_port] => 

The issue I'm having is getting around the following inside the DBConfig.php file:

/**
 * ExpressionEngine Database Config Class
 *
 * @package    ExpressionEngine
 * @subpackage Core
 * @category   Core
 * @author     EllisLab Dev Team
 * @link       https://ellislab.com
 */
class DBConfig implements Config {

    protected $delegate;
    protected $active_group;

    protected $defaults = array(
        'port'     => 3306,
        'hostname' => '127.0.0.1',
        'username' => 'root',
        'password' => '',
        'database' => '',
        'dbdriver' => 'mysqli',
        'pconnect' => FALSE,
        'dbprefix' => 'exp_',
        'swap_pre' => 'exp_',
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'autoinit' => FALSE,
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_unicode_ci',
        'cachedir' => '', // Set in constructor
    );

    /**
     * Create new Database Config object
     *
     * @param ConfigFile $config Config\File object
     */
    public function __construct(ConfigFile $config)
    {
        $this->delegate = $config;
        $this->active_group = $this->delegate->get(
            'database.active_group',
            'expressionengine'
        );

        $this->defaults['cachedir'] = rtrim(APPPATH, '/').'/user/cache/db_cache/';
    }

Is there a way to dynamically pass the $active_group into the DBConfig class?

| improve this question | | | | |
1

In your dynamic config, define the $config['database']['expressionengine'] group conditionally. Have a look TJ Draper's dynamic config, specifically these lines

// Set up the database
$db = array(
    'hostname' => 'localhost',
    'dbdriver' => 'mysqli',
    'dbprefix' => 'exp_',
    'pconnect' => FALSE
);

// Require environment config file
require 'config.' . ENV . '.php';

// Set the database array to the config
$config['database'] = array (
    'expressionengine' => $db,
);

In your config.foo.php files, specify the $db array, overriding any settings necessary:

$db['username'] = 'xxxxxxxxxxxxxx';
$db['password'] = 'xxxxxxxxxxxxxx';
$db['database'] = 'xxxxxxxxxxxxxx';
| improve this answer | | | | |
  • Right. So the 'active_group' HAS to be 'expressionengine' though? As where in 2, you could specify in the top of the database.php file $active_group = 'expressionengine'; (or whatever else you set in the $db array there.) like: $db['local']['hostname'] = 'localhost'; $db['local']['username'] = ‘mylocaluser’; where you could set it as $active_group = 'local'; Is this right? – W3bGuy Jun 28 '16 at 23:22
  • Yep! This way ExpressionEngine only connects through a consistently named group. Having a group at all is a bit of legacy. – Derek Jones Jun 28 '16 at 23:29
1

@derek-jones Thanks for the nudge... I ended up with the following in mine.

/*
|-----------------------------------------------------------
| Database and Multi-environment
|-----------------------------------------------------------
| You can have whatever environments you need.
| First, build out your environment arrays.
| Then set your $active_environment to which you need to use.
*/
    $active_environment = 'production';

    $localhost = array (
        'hostname' => 'localhost',
        'username' => 'local-un',
        'password' => 'local-pw',
        'database' => 'local-db',
        'dbdriver' => 'mysqli',
        'dbprefix' => 'exp_',
        'pconnect' => FALSE
    );

    $production = array (
        'hostname' => 'remote-host',
        'username' => 'remote-un',
        'password' => 'remote-pw',
        'database' => 'remote-db',
        'dbdriver' => 'mysqli',
        'dbprefix' => 'exp_',
        'pconnect' => FALSE
    );

    $config['database'] = array (
        'expressionengine' => $$active_environment
    );
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.