Problem creating "Zend_Session"

6

I'm trying to create a session in Zend as follows:

if (isset($_POST['login'])) {

  Zend_Session::start();

  $session = new Zend_Session_Namespace(); 

  $login = explode('-', $_POST['login']);

  $codigo = $login[1];

  $banco = $admin->fetchAll('SELECT banco FROM tbl_clientes WHERE codigo = ' . $codigo);

  Zend_Registry::set('banco', $banco[0]['banco']); 

  $session->banco = $banco[0]['banco'];
}

However when trying to access the $session->banco variable on another page it just does not exist!

OBS.
As soon as I assign the value of it, it gets the value of $banco[0]['banco'] . Normally, I need this value to be able to set defaultAdapter of Zend_Db_Table and make it available in the application so I can access the database. This bank will be on a record in the query that I'm doing above.

I was trying to handle all this in Bootstrap.php but I saw that it is not very sure, any way I'm having trouble saving this information, the bank returns me the following error:

'No adapter found for models_DbTable_TblChamado'

If there is a better way to do this, I'm open to suggestions.

    
asked by anonymous 24.01.2014 / 00:05

1 answer

2

I decided as follows, in% with% done all the configuration, first I check if the login exists, I consult the data in the bank, save the values to access the specific user's bank and saved in the session:

        Zend_Session::start();

        $bd = $this->getPluginResource('db');
        $params = $bd->getOptions();

        $admin = new Zend_Db_Adapter_Pdo_Mysql($params['params']);

        $bd = Zend_Db::factory('Pdo_Mysql', $params['params']);
        Zend_Registry::set('Db_admin', $bd);
        $sessionDb = new Zend_Session_Namespace('banco');

        /**
         * Inicia a base de dados do aplicativo com o usuario. 
         */
        if(isset($_POST['login'])) {
            $login = explode('-', $_POST['login']);
            $codigo = $login[1];
            $banco = $admin->fetchAll('SELECT banco FROM tbl_clientes WHERE codigo = ' . $codigo);
            Zend_Registry::set('banco', $banco[0]['banco']); 
            $sessionDb->banco = Zend_Registry::get('banco');
        }

        $nomebanco = $sessionDb->banco;

        //se existe os dados de login eu pego os dados do banco do 
            //cliente e registro pra que fique disponível em toda a app
            if ($nomebanco) {
                $pdoParams = array(
                    PDO::ATTR_PERSISTENT => true,
                    PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
                    PDO::ATTR_EMULATE_PREPARES => true,
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');

                $database = array(
                    'host' => 'localhost',
                    'username' => 'root',
                    'password' => '',
                    'dbname' => $sessionDb->banco,
                    'driver_options' => $pdoParams
                );
                $db = Zend_Db::factory('Pdo_Mysql', $database);
                Zend_Db_Table::setDefaultAdapter($db);
                Zend_Registry::set('Zend_Db', $db);

            }
    
27.01.2014 / 23:08