2011-07-13 55 views
1

我有非常奇怪的錯誤,我無法理解。我開始了一個新的項目拋出zf.sh. 現在,當我嘗試從數據庫中獲取數據時,我會在我嘗試從中獲取數據時創建的每個表都出錯。未找到適用於App_Model_DbTable_User的適配器

在這種情況下,我嘗試獲取數據庫中存在的用戶。

錯誤:

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\''); 
    } 

} 

user.php的在模型/ DBTABLE

<?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; 
    } 

有人誰可以幫我,看看有什麼不對的代碼?
Thx!

+0

您是否嘗試將適配器更改爲「pdo_mysql」?另外,似乎數據庫參數只是爲了開發而定義的,而不是爲了生產。不知道這是否重要。 – Marcin

+0

我嘗試過使用「pdo_mysql」,它是一樣的。我爲我的應用程序開發了環境,但在我的代碼中,我在生產中擁有相同的數據庫參數,沒有任何區別。 – jarnesjo

+0

如果你要堅持使用mysqli,只需在你的app.ini中使用'resources.db.params.charset = utf8'並移除$ db-> query('SET CHARACTER SET \'UTF8 \''); '線。 – nevvermind

回答

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

0

我固定在我的項目這個問題放的application.ini代碼塊dbAdpter到標籤生產

[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] 
相關問題