如何在php

时间:2015-06-02 14:07:44

标签: php mysql symfony configuration doctrine-orm

我有一个带有Doctrine的Symfony 2.7应用程序。

我正在尝试设置从属数据库。

我们使用lockerbox来管理我们的服务器配置。

我们使用Constants来保留所有数据库配置,例如:

<?php
define('DB_MASTER_HOST', 'localhost');
define('DB_MASTER_USER', 'user');
define('DB_MASTER_PASS', 'password');
define('DB_MASTER_NAME', 'db_name');

define('DB_SLAVE_ENABLED', FALSE);
define('DB_SLAVE_HOST', 'server-1,server-2');
define('DB_SLAVE_USER', 'username1,username2');
define('DB_SLAVE_PASS', 'password1,password2');
define('DB_SLAVE_NAME', 'db_name,db_name');

我们可以拥有多个从属数据库,配置以逗号分隔。

由于lockerbox配置可以随时更改而且lockerbox配置不可编辑以复制Symfony YAML格式,我需要一种方法来设置带有parameters.php的配置。

我能够以标准的Symfony方式手动配置slave数据库:

...other configs...
# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        keep_slave: true
        slaves:
            slave1:
                dbname: db_slave_name
                host: db_slave_server
                user: username
                password: password

...other configs

到目前为止,我已经尝试了这个,但它不起作用

<?php

// set default database connection info
$container->setParameter('database_host', DB_MASTER_HOST);
$container->setParameter('database_user', DB_MASTER_USER);
$container->setParameter('database_password', DB_MASTER_PASS);
$container->setParameter('database_name', DB_MASTER_NAME);

if (DB_SLAVE_ENABLED) {
    $slave_hosts = explode(',', DB_SLAVE_HOST);
    $slave_names = explode(',', DB_SLAVE_NAME);
    $slave_users = explode(',', DB_SLAVE_USER);
    $slave_passwords = explode(',', DB_SLAVE_PASS);

    $slave_count = min(count($slave_hosts), count($slave_names), count($slave_passwords), count($slave_users));

    if ($slave_count) {
        // add all slaves to config

        error_log('we have slaves and they are enabled');

        $container->setParameter('doctrine.dbal.slaves.slave2.host', $slave_hosts[0]);
        $container->setParameter('doctrine.dbal.slaves.slave2.name', $slave_names[0]);
        $container->setParameter('doctrine.dbal.slaves.slave2.user', $slave_users[0]);
        $container->setParameter('doctrine.dbal.slaves.slave2.password', $slave_passwords[0]);
    }

}   // end DB_SLAVE_ENABLED

如何在Symfony 2.7中以编程方式设置多个从数据库?

1 个答案:

答案 0 :(得分:1)

您可以在parameters.php中创建您的从属配置数组,并直接设置从属参数。

parameters.php

if (DB_SLAVE_ENABLED) {
    // ...
    if ($slave_count) {
        // add all slaves to config

        error_log('we have slaves and they are enabled');

        $slaves = array();

        for ($i = 0; $i < $slave_count; $i++) {
            $slaves['slave_'.$i] = array(
                'host'      => $slave_hosts[$i],
                'dbname'    => $slave_names[$i],
                'user'      => $slave_users[$i],
                'password'  => $slave_passwords[$i],

            );
        }

        $container->setParameter('database_slaves', $slaves);
    }
}

config.yml

...other configs...
# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        keep_slave: true
        slaves:   "%database_slaves%"

...other configs