Switch in Application.ini ZF 1 configuration

Asked

Viewed 96 times

0

Inside the Zend Framework 1 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 you to HOST is dynamic. It will be changed as you choose on a Switch in 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 it work in this idea?

  • Problem was solved based on this zend link. http://framework.zend.com/manual/1.12/en/zend.config.writer.introduction.html

1 answer

1

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

Add connections to 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 file "application/Bootstrap.php":

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 just inform on each model which is the corresponding connection:

class Application_Model_DbTable_NomeTabela extends Zend_Db_Table_Abstract
{
    protected $_schema = "nome_banco_1";
    protected $_name = "nome_tabela";
}
  • But the only thing that changes is the host, the other data is the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.