我有非常奇怪的錯誤,我無法理解。我開始了一個新的項目拋出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!
您是否嘗試將適配器更改爲「pdo_mysql」?另外,似乎數據庫參數只是爲了開發而定義的,而不是爲了生產。不知道這是否重要。 – Marcin
我嘗試過使用「pdo_mysql」,它是一樣的。我爲我的應用程序開發了環境,但在我的代碼中,我在生產中擁有相同的數據庫參數,沒有任何區別。 – jarnesjo
如果你要堅持使用mysqli,只需在你的app.ini中使用'resources.db.params.charset = utf8'並移除$ db-> query('SET CHARACTER SET \'UTF8 \''); '線。 – nevvermind