找不到App_Model_DbTable_User的适配器

时间:2011-07-13 14:05:07

标签: php zend-framework zend-db-table

我有一个非常奇怪的错误,我无法理解。我开始一个新项目抛出zf.sh. 现在,当我尝试从数据库中获取数据时,我在尝试从中获取数据时创建的每个表都会出错。

在这种情况下,我尝试获取数据库中存在的用户。

错误:

Message: No adapter found for App_Model_DbTable_User
Stack trace:
#0 /Applications/MAMP/htdocs/offert/library/Zend/Db/Table/Abstract.php(739): Zend_Db_Table_Abstract->_setupDatabaseAdapter()
#1 /Applications/MAMP/htdocs/offert/library/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract->_setup()
#2 /Applications/MAMP/htdocs/offert/application/models/UserMapper.php(9): Zend_Db_Table_Abstract->__construct()
#3 /Applications/MAMP/htdocs/offert/application/models/UserMapper.php(20): App_Model_UserMapper->setDbTable('App_Model_DbTab...')
#4 /Applications/MAMP/htdocs/offert/application/models/UserMapper.php(69): App_Model_UserMapper->getDbTable()
#5 /Applications/MAMP/htdocs/offert/application/controllers/AuthController.php(23): App_Model_UserMapper->find(3)
#6 /Applications/MAMP/htdocs/offert/library/Zend/Controller/Action.php(513): AuthController->loginAction()
#7 /Applications/MAMP/htdocs/offert/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('loginAction')
#8 /Applications/MAMP/htdocs/offert/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#9 /Applications/MAMP/htdocs/offert/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#10 /Applications/MAMP/htdocs/offert/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#11 /Applications/MAMP/htdocs/offert/public/index.php(26): Zend_Application->run()
#12 {main}  
Request Parameters:
array (
  'controller' => 'auth',
  'action' => 'login',
  'module' => 'default',
)  

的application.ini

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

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =

appnamespace = "App"

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

resources.db.adapter = "MYSQLI"
resources.db.params.dbname = "offert"
resources.db.params.host = "127.0.0.1"
resources.db.params.port = "8889"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.isDefaultTableAdapter = true

bootstrap.php中

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    // Init Autoload
    protected  function _initAutoload() {
        $autoloader = new Zend_Application_Module_Autoloader(array(
                'namespace' => 'App',
                'basePath' => dirname(__FILE__),
        ));

        $autoloader->addResourceType('acl', 'acl/', 'Acl');
        $autoloader->addResourceType('classes', 'classes', 'Class');

        return $autoloader;
    }

    // Plugin Controller
    protected function _initControllerPlugin() {
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new App_Plugin_Controller());
    }

    // Doctype
    protected function _initDoctype() {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    // Db
    protected function _initDb() {
        $resource = $this->getPluginResource('db');
        $db = $resource->getDbAdapter();
        $db->query('SET CHARACTER SET \'UTF8\'');
    }

}
models / DbTable中的

User.php

<?php

class App_Model_DbTable_User extends Zend_Db_Table_Abstract {
    protected $_name = "user";
    protected $_primary = 'userID';
}

UserMapper.php

<?php

class App_Model_UserMapper {

    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('App_Model_DbTable_User');
        }
        return $this->dbTable;
    }

    /**
     * Find specified user
     * @param $id
     * @return App_Model_User
     */
    public function find($id) {

        $result = $this->getDbTable()->find($id);

        if(0 == count($result)) return;

        $row = $result->current();

        $user = new App_Model_User();
        $user->setUserID($row->userID)
            ->setFirstname($row->firstname)
            ->setLastname($row->lastname)
            ->setEmail($row->email)
            ->setPassword($row->password)
            ->setTelephone($row->telephone)
            ->setRole($row->role)
            ->setActive($row->active)
            ->setCreated($row->created)
            ->setLastLogin($row->lastLogin)
            ->setCompanyID($row->companyID);
        return $user;
    }

有人可以帮助我,看看代码有什么问题吗? 谢谢!

2 个答案:

答案 0 :(得分:1)

问题在于您不是引导db资源。通过在Bootstrap中创建一个名为_initDb的方法,告诉Bootstrap您的方法将处理db资源引导程序。因此,Bootstrap永远不会调用$this->bootstrap('db')

如果您尝试使用名称$this->bootstrap('db')在方法中添加_initDb,那么您将获得例外Circular resource dependency detected

所以,解决方案应该是:

protected function _initMyDb() {
    $this->bootstrap('db');
    $resource = $this->getPluginResource('db');
    $db = $resource->getDbAdapter();
    $db->query('SET CHARACTER SET \'UTF8\'');
}

启动后,您可以开始使用Zend_Db_Table::getDefaultAdapter()

答案 1 :(得分:0)

我在我的项目中解决了这个问题,将application.ini的代码块dbAdpter放入标签 production

[production]

resources.db.adapter = "mysqli"
resources.db.params.host = "localhost"
resources.db.params.username = "db_user"
resources.db.params.password = "db_pass"
resources.db.params.dbname = "zf_cms"
resources.db.isDefaultTableAdapter = true

[staging : production]