1

我正在学习 Zend 框架工作,同时通过教程我遇到了 No adapter found for 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';
}

其他用户类为空。

索引控制器

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

`提前谢谢你们,我真的不知道从哪里开始新的框架。我用谷歌搜索了所有东西,即将重新启动 tut,但我觉得又遇到了这个问题。

谢谢

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
4

3 回答 3

1

如果我没记错的话,您需要告诉您的应用程序始终使用数据库连接作为 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();
}
于 2012-05-18T12:00:19.187 回答
0

在您的构造函数中,您似乎正在尝试初始化一个新的数据库适配器。DbTable 模型已使用application.ini中的适配器集,因此无需设置新适配器。另外我不确定它是否重要,但手册中适配器的语法是* Pdo_Mysql *
如果您需要使用多个适配器,那么在.application.ini

综上所述,使用 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 接口来编写查询。

在试图弄清楚这些东西时,我发现这些链接很有帮助。

集成数据映射器的领域模型

你总能在Rob Allen 的 Akratbat.com上找到很棒的东西

祝你好运。

于 2012-05-18T14:33:18.687 回答
0

当文件移动到开发服务器时,尽管它在本地服务器上运行,我也面临同样的问题。然后我只是遵循文件命名约定。

  1. 使用以下所有内容,请注意小写/大写
  2. 请注意,模型文件夹是复数,而类是模型单数
  3. 确保该类被命名为 Application_Model_DbTable_Albums
  4. 确保文件名为 Albums.php 并位于名为 application/models/DbTable 的文件夹中

您必须仔细检查并检查文件是否正确命名(例如,模型的 Users.php)。

它解决了我的问题。也许是你的。

祝你好运。

于 2014-11-27T11:57:47.087 回答