Zend Framework:初始化会话(数据库)错误

时间:2012-04-17 19:45:52

标签: php zend-framework

  

致命错误:带有消息的未捕获异常'Zend_Db_Table_Exception'   '找不到适用于Zend_Session_SaveHandler_DbTable的适配器'   第755行的C:\ wamp \ www \ hol \ library \ Zend \ Db \ Table \ Abstract.php(!)   Zend_Db_Table_Exception:找不到适配器   Zend_Session_SaveHandler_DbTable in   第755行的C:\ wamp \ www \ hol \ library \ Zend \ Db \ Table \ Abstract.php调用   堆

     

时间记忆功能位置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

代码:

//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();

配置

<?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`)
);

2 个答案:

答案 0 :(得分:2)

您没有发布config.php的内容,但根据错误我怀疑您没有指定适配器(mysql,pdo等)。

我的配置文件看起来像(yaml):

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

根据Zend API docs,你必须像这样指定适配器:

// 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));

答案 1 :(得分:1)

您需要调用Zend_Db_Table_Abstract::setDefaultAdapter($db)提供您在配置示例中创建的$db对象,因为看起来您没有在给定错误的情况下设置默认数据库适配器,或者您需要将$db对象添加到$sess_config数组,以便将其设置为Zend_Session的数据库适配器。

Zend_Session_SaveHandler_DbTable扩展Zend_Db_Table_Abstract,然后将Zend_Session_SaveHandler_DbTable未知的任何选项(例如数据库配置选项)传递给设置DbTable的Zend_Db_Table_Abstract构造函数。

试试这个:

$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',
);