John
John

Reputation: 3050

Zend Framework: Initialize session (Database) error

Fatal error: Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for Zend_Session_SaveHandler_DbTable' in C:\wamp\www\hol\library\Zend\Db\Table\Abstract.php on line 755 ( ! ) Zend_Db_Table_Exception: No adapter found for Zend_Session_SaveHandler_DbTable in C:\wamp\www\hol\library\Zend\Db\Table\Abstract.php on line 755 Call Stack

Time Memory Function Location 1 0.0005 373664 {main}( ) ..\init.php:0

2 0.0325 2749720 Zend_Session_SaveHandler_DbTable->__construct( ) ..\init.php:40 3 0.0325 2750168 Zend_Db_Table_Abstract->__construct( ) ..\DbTable.php:207 4 0.0325 2750480 Zend_Session_SaveHandler_DbTable->_setup( ) ..\Abstract.php:268 5 0.0325 2750480 Zend_Db_Table_Abstract->_setup( ) ..\DbTable.php:403 6 0.0325 2750480 Zend_Db_Table_Abstract->_setupDatabaseAdapter( ) ..\Abstract.php:739

Code:

//Configuration consumption
$config = new Zend_Config(require 'config.php');

//Database configuration
$db = Zend_Db::factory($config->database->adapter, array(
    'host'     => $config->database->params->host,
    'username' => $config->database->params->username,
    'password' => $config->database->params->password,
    'dbname'   => $config->database->params->dbname
));

$sess_config = array(
    'name'              => 'session',
    'primary'           => array(
        'session_id',
        'save_path',
        'name',
    ),
    'primaryAssignment' => array(
        'sessionId',
        'sessionSavePath',
        'sessionName',
    ),
    'modifiedColumn'    => 'modified',
    'dataColumn'        => 'session_data',
    'lifetimeColumn'    => 'lifetime',
);

Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($sess_config));

//Initialize the session
Zend_Session::start();

config

<?php
// config.php
return array(
    'database' => array(
        'adapter' => 'Pdo_Mysql',
        'params'  => array(
            'host'     => 'localhost',
            'username' => 'root',
            'password' => '',
            'dbname'   => 'hol'
        )
    ),
    'session' => array(
        'name'              => 'session',
        'primary'           => array(
            'session_id',
            'save_path',
            'name',
        ),
        'primaryAssignment' => array(
            'sessionId',
            'sessionSavePath',
            'sessionName',
        ),
        'modifiedColumn'    => 'modified',
        'dataColumn'        => 'session_data',
        'lifetimeColumn'    => 'lifetime'
    )
);

SQL:

CREATE TABLE `session` (
    `session_id` char(32) NOT NULL,
    `save_path` varchar(32) NOT NULL,
    `name` varchar(32) NOT NULL DEFAULT '',
    `modified` int,
    `lifetime` int,
    `session_data` text,
    PRIMARY KEY (`Session_ID`, `save_path`, `name`)
);

Upvotes: 1

Views: 1035

Answers (2)

drew010
drew010

Reputation: 69927

You either need to call Zend_Db_Table_Abstract::setDefaultAdapter($db) providing the $db object you created in your configuration example, as it appears you don't have a default DB adapter set up given the error, or you need to add the $db object to the $sess_config array so it gets set up as the DB adapter for Zend_Session.

Zend_Session_SaveHandler_DbTable extends Zend_Db_Table_Abstract and any options unknown to Zend_Session_SaveHandler_DbTable (e.g. database configuration options) are then passed to the constructor of Zend_Db_Table_Abstract which sets up the DbTable.

Try this:

$sess_config = array(
    'db'                => $db, // Pass the $db adapter as the 'db' parameter to Zend_Db_Table_Abstract
    'name'              => 'session',
    'primary'           => array(
        'session_id',
        'save_path',
        'name',
    ),
    'primaryAssignment' => array(
        'sessionId',
        'sessionSavePath',
        'sessionName',
    ),
    'modifiedColumn'    => 'modified',
    'dataColumn'        => 'session_data',
    'lifetimeColumn'    => 'lifetime',
);

Upvotes: 1

Mike Purcell
Mike Purcell

Reputation: 19979

You didn't post the contents of your config.php, but based on the error I suspect you did not specify an adapter (mysql, pdo, etc).

My config file looks something like (yaml):

database:
  adapter: Pdo_Mysql
  params:
    host:     myhost
    dbname:   mydb
    username: myusername
    password: mypassword

According to the Zend API docs, you have to specify the adapter like this:

// Set this before you make the call to setSaveHandler()
Zend_Db_Table_Abstract::setDefaultAdapter($db);

Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($sess_config));

Upvotes: 2

Related Questions