无法在Zend中连接到MySQL

时间:2011-01-15 06:26:59

标签: php mysql zend-framework

我正在尝试使用this tutorial在zend应用程序中连接mysql表(Authentication)。但是无法获得DbTable的实例。我只在我的应用程序中更改了directory structure

我有 Mapper 这样的东西:

class Model_Authentication_Mapper {

    protected $_dbTable;

    public function setDbTable( $dbTable ) {

        if (is_string( $dbTable ) ) {
            $dbTable = new $dbTable();
        }
        if ( !$dbTable instanceof Zend_Db_Table_Abstract ) {
            throw new Exception( 'Invalid table data gateway provided' );
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable() {

        if (null === $this->_dbTable) {
            $this->setDbTable( 'Model_Authentication_DbTable' );
        }
        return $this->_dbTable;
    }

    public function getRecordById( $id ) {
     $table = $this->getDbTable();
     // Other code here
    }

}

DbTable 就像这样:

class Model_Authentication_DbTable extends Zend_Db_Table_Abstract {
    protected $_name    = 'Authentication';
}

当它在Mapper中执行$table = $this->getDbTable();时,它会在firebug控制台中显示以下错误:

An error occurred
Application error
Message: No adapter found for Model_Authentication_DbTable

如何为此DbTable设置适配器?

任何人都知道这个??

由于

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用zf工具?以下命令可以帮助您在MySQL数据库的生产部分中创建数据库适配器:

zf.sh configure db-adapter "adapter=PDO_MYSQL&host=localhost&dbname=test&username=testuser&password=testpasswort&charset=utf8" production

请确保您的计算机上安装了适用于MySQL的PDO驱动程序。

答案 1 :(得分:0)

找到解决方案。我只是注释掉application.ini中的一些行,一切正常:

[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.params.displayExceptions = 1

;[staging : production]

;[testing : production]
;phpSettings.display_startup_errors = 1
;phpSettings.display_errors = 1

;[development : production]
;phpSettings.display_startup_errors = 1
;phpSettings.display_errors = 1
;resources.frontController.params.displayExceptions = 1

resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "bbname"
resources.db.isDefaultTableAdapter = true