Switch in ZF 1 Application.ini configuration

0

Inside the Zend Framework Application.ini I have the following configuration:

resources.multidb.sga.adapter = "PDO_MYSQL"
resources.multidb.sga.host = "host.acula.net"
resources.multidb.sga.username  = "usuario"
resources.multidb.sga.password  = "senha"
resources.multidb.sga.port  = "3306"
resources.multidb.sga.dbname  = "base"

But I need HOST to be dynamic . It will be changed as you choose in a Switch on the Controller.

switch ($unidade) {
            case 'Gama':

                $host = 'sgagama.servidor.br';
                $unidade = 5;

                $this->realizaConsultaSga($mes, $ano, $unidade);

                break;

        case 'Sede':

                $host = 'sgasede.servidor.br';
                $unidade = 1;

                $this->realizaConsultaSga($mes, $ano, $unidade);

        break;
....

How do I make this work for this idea?

    
asked by anonymous 04.11.2014 / 19:18

1 answer

1

You can modify the database settings in the bootstrap. In this link has an example of how to use multiple databases. Just tell us which database you want to use.

  

Add the connections in your file   "Application / configs / application.ini":

resources.db.nome_banco_1.adapter = "PDO_MYSQL"
resources.db.nome_banco_1.params.host = "127.0.0.1"
resources.db.nome_banco_1.params.username = "root"
resources.db.nome_banco_1.params.password = "root"
resources.db.nome_banco_1.params.dbname = "nome_banco_1"
resources.db.nome_banco_1.charset = "UTF-8"
resources.db.nome_banco_1.isDefaultTableAdapter = true
resources.db.nome_banco_2.adapter = "PDO_MYSQL"
resources.db.nome_banco_2.params.host = "127.0.0.1"
resources.db.nome_banco_2.params.username = "root"
resources.db.nome_banco_2.params.password = "root"
resources.db.nome_banco_2.params.dbname = "nome_banco_2"
resources.db.nome_banco_2.charset = "UTF-8"
     

Change the "application / Bootstrap.php" file:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initDb()
    {
        $config = new Zend_Config_Ini('application/configs/application.ini');
        $dbAdapters = array();
        foreach($config->{APPLICATION_ENV}->resources->db as $config_name => $db){
            $dbAdapters[$config_name] = Zend_Db::factory($db->adapter,$db->params->toArray());
            if((boolean)$db->isDefaultTableAdapter){
                Zend_Db_Table::setDefaultAdapter($dbAdapters[$config_name]);
            }
        }
    }
}
     

And now you just have to tell in each model what the corresponding connection is:

class Application_Model_DbTable_NomeTabela extends Zend_Db_Table_Abstract
{
    protected $_schema = "nome_banco_1";
    protected $_name = "nome_tabela";
}
    
04.11.2014 / 19:29