Problem creating "Zend_session"

Asked

Viewed 322 times

6

I’m trying to create a session on 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'];
}

But when trying to access the variable $session->banco on another page it simply doesn’t exist!

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

I was trying to treat all this in the Bootstrap.php but I saw that it is not very certain, somehow I am struggling to save this information, the bank returns me the following error:

'No adapter found for models_DbTable_TblChamado'

If there’s a better way to do that, I’m open to suggestions.

  • On the other page you ran the Zend_Session::start(); again before accessing the $session->banco?

  • I recommend you take a look at the ZF2. It was nice mt, mainly the Servicemanager and Eventmanager schemes. They are powerful mt tools, especially if used together with the new modules system

1 answer

2


I resolved it as follows, in Bootstrap.php I did all the configuration, first I check if there is the login, check the data in the database, save the values to access the specific user’s database 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);

            }

Browser other questions tagged

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