我正在学习Zend框架的工作,而通过一个教程我跑进了一个找不到Application_Model_DbTable_Users错误的适配器。我似乎无法弄明白。 这是我的课程
class Application_Model_Register
{
private $_dbTable;
private $db;
public function __construct()
{
$this->_dbTable = new Application_Model_DbTable_Users();
$parameters =array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '******',
'dbname' => 'tutblog',
'adapter' => 'Pdo_Mysql'
);
try {
$db = Zend_Db::factory('Pdo_Mysql', $parameters);
$db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
} catch (Zend_Exception $e)
}
public function createUser($array)
{ var_dump($array);
$_dbTable = new Application_Model_DbTable_Users();
$this->_dbTable->insert($array);
//insert('array') SQL commands-> form zen_db_table_abstract
}
DBtable用户的班级
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
protected $_primary ='id';
protected $_password= 'password';
}
其他用户类为空。
的IndexController
public function indexAction()
{
$register = new Application_Model_Register();
$register ->createUser(array( 'name' => 'becky',
'age' => 25,
'password' => 'somepw'
)2);*/
}
我的ini文件
[production]
phpSettings.display_startup_errors = 2
phpSettings.display_errors = 2
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.db.params.adapter_ = "PDO_MYSQL"
resources.db.params.host ='localhost'
resources.db.params.dbname ='tutblog'
resources.db.params.username = "root"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true
[staging : 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.params.adapter_ = "PDO_MYSQL"
resources.db.params.host ='localhost'
resources.db.params.dbname ='tutblog'
resources.db.params.username = "root"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true
`提前谢谢你们,我真的不知道从哪里开始新的框架。我用谷歌搜索了所有东西,即将重新启动啧啧,但我觉得再次陷入这个问题。
日Thnx
Message: No adapter found for Application_Model_DbTable_Users
堆栈追踪:
0 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Db/Table/Abstract.php(739): Zend_Db_Table_Abstract->_setupDatabaseAdapter()
#1 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract->_setup()
#2 /Applications/XAMPP/xamppfiles/htdocs/webs/zfNewTry/application/models/Register.php(10): Zend_Db_Table_Abstract->__construct()
#3 /Applications/XAMPP/xamppfiles/htdocs/webs/zfNewTry/application/controllers/IndexController.php(15): Application_Model_Register->__construct()
#4 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Controller/Action.php(513): IndexController->indexAction()
#5 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Controller/Dispatcher/Standard.php(289): Zend_Controller_Action->dispatch('indexAction')
#6 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#7 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#8 /Applications/XAMPP/xamppfiles/lib/php/pear/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#9 /Applications/XAMPP/xamppfiles/htdocs/webs/zfNewTry/public/index.php(26): Zend_Application->run()
#10 {main}
Request Parameters:
array (
'controller' => 'index',
'action' => 'index',
'module' => 'default',
)
zf enable layout to get this template
答案 0 :(得分:1)
如果我没记错的话,你需要告诉应用程序始终使用db连接作为Zend_Db_Table类的默认连接。
尝试在数据库初始化之后放置此行:
Zend_Db_Table::setDefaultAdapter($db)
我认为你应该在实例化Zend_Db_Table类之前执行此操作,因此将Application_Model_Register :: __ construct()更改为:
public function __construct()
{
$parameters =array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '******',
'dbname' => 'tutblog',
'adapter' => 'Pdo_Mysql'
);
try {
$db = Zend_Db::factory('Pdo_Mysql', $parameters);
$db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
} catch (Zend_Exception $e) {
}
Zend_Db_Table::setDefaultAdapter($db);
$this->_dbTable = new Application_Model_DbTable_Users();
}
答案 1 :(得分:0)
在你的构造函数中,你似乎试图初始化一个新的db适配器。 DbTable模型已使用 application.ini 中设置的适配器,因此无需设置新适配器。另外我不确定这是否重要,但手册中适配器的语法是 * Pdo_Mysql *
如果您需要使用多个适配器,那么使用application.ini
中的multiDb资源会更加一致。
所有人都说,使用DbTable模型的模型的构造函数通常看起来类似于:
//This class is currently intended to be extended by concrete mappers
protected $_tableGateway = NULL;
protected $_identityMap = array();
//this constructor typically provides the option to pass in a DbTable model
// or a string value for the table name that would instantiate a Zend_Db_Table_Abstract
public function __construct(Zend_Db_Table_Abstract $tableGateway = NULL) {
if (is_null($tableGateway)) {
//_tablename is the string value of the table, supplied by the concrete mapper
$this->_tableGateway = new Zend_Db_Table($this->_tableName);
} else {
$this->_tableGateway = $tableGateway;
}
}
protected function _getGateway() {
return $this->_tableGateway;
}
/**
* findAll() is a proxy for the fetchAll() method and returns
* an array of entity objects
*
* @return array returns an array of row objects
*/
public function findAll() {
$select = $this->_getGateway()->select();
$rowset = $this->_getGateway()->fetchAll($select);
$entities = array();
foreach ($rowset as $row) {
$entity = $this->_createEntity($row);
$entities[] = $entity;
}
return $entities;
}
/**
* Abstract method to be implemented by concrete mappers.
*/
abstract protected function _createEntity($row);
}
我提供了一些方法来帮助演示如何使用Zend_Db_Table_Abstract接口来编写查询。
我发现这些链接在尝试解决这些问题时很有帮助。
Domain Models
Integrating Data Mappers
祝你好运。
答案 2 :(得分:0)
当文件移动到开发服务器但我在本地服务器上运行时,我也遇到了同样的问题。然后我就按照文件命名约定。
你必须经历它并检查文件是否正确命名(例如,模型的Users.php)。
它解决了我的问题。也许是你的。
祝你好运。