Zend framework 强制Zend Framework比通常更早地连接到数据库

Zend framework 强制Zend Framework比通常更早地连接到数据库,zend-framework,zend-db,Zend Framework,Zend Db,我试图在数据库中存储Zend框架的自定义路由。我已经关闭了创建路由的进程,但是我遇到的问题是,当我添加路由时,Zend似乎还没有创建到数据库的连接 有人知道这个过程最初发生在哪里,或者我如何从Bootstrap.php中的init_routes函数强制数据库连接吗 更新: 我在Bootstrap.php中所做的是调用一个模型,该模型将为供应商返回所有Zend_Controller_Router_Route_静态对象。下面是我在Bootstrap.php中使用的代码 $frontControlle

我试图在数据库中存储Zend框架的自定义路由。我已经关闭了创建路由的进程,但是我遇到的问题是,当我添加路由时,Zend似乎还没有创建到数据库的连接

有人知道这个过程最初发生在哪里,或者我如何从Bootstrap.php中的init_routes函数强制数据库连接吗

更新:

我在Bootstrap.php中所做的是调用一个模型,该模型将为供应商返回所有Zend_Controller_Router_Route_静态对象。下面是我在Bootstrap.php中使用的代码

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();

$vendor_routes = new Application_Model_Vendor();
$vendor_routes = $vendor_routes->getStaticRoutes();
getStaticRoutes()函数中的代码如下所示:

public function getStaticRoutes() {
    $select = $this->select();
    $select->from($this)
        ->where("featured = 1");
    $rows = $this->fetchAll($select);

    foreach($rows as $row) {
        print_r($row);
    }
}
<b>Fatal error</b>:  Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for Application_Model_Vendor' in /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php:754
Stack trace:
#0 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php(739): Zend_Db_Table_Abstract-&gt;_setupDatabaseAdapter()
#1 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract-&gt;_setup()
#2 /var/www/vhosts/weddingdir/weddingdir/application/Bootstrap.php(17): Zend_Db_Table_Abstract-&gt;__construct()
#3 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(666): Bootstrap-&gt;_initRoutes()
#4 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(619): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_executeResource('routes')
#5 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(583): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_bootstrap(NUL in <b>/var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php</b> on line <b>754</b><br /> 
此函数包含在扩展Zend_Db_Table_Abstract的模型中

我得到的错误如下:

public function getStaticRoutes() {
    $select = $this->select();
    $select->from($this)
        ->where("featured = 1");
    $rows = $this->fetchAll($select);

    foreach($rows as $row) {
        print_r($row);
    }
}
<b>Fatal error</b>:  Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for Application_Model_Vendor' in /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php:754
Stack trace:
#0 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php(739): Zend_Db_Table_Abstract-&gt;_setupDatabaseAdapter()
#1 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php(268): Zend_Db_Table_Abstract-&gt;_setup()
#2 /var/www/vhosts/weddingdir/weddingdir/application/Bootstrap.php(17): Zend_Db_Table_Abstract-&gt;__construct()
#3 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(666): Bootstrap-&gt;_initRoutes()
#4 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(619): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_executeResource('routes')
#5 /var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(583): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_bootstrap(NUL in <b>/var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php</b> on line <b>754</b><br /> 
致命错误:未捕获异常“Zend_Db_Table_exception”,在/var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php:754中显示消息“未找到应用程序模型供应商的适配器”
堆栈跟踪:
#0/var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php(739):Zend_Db_Table_Abstract-_setupDatabaseAdapter()
#1/var/www/vhosts/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php(268):Zend_Db_Table_Abstract-_setup()
#2/var/www/vhosts/weddingdir/weddingdir/application/Bootstrap.php(17):Zend_Db_Table_Abstract-_construct()
#3/var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(666):Bootstrap-_initRoutes()
#4/var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(619):Zend_Application_Bootstrap_BootstrapAbstract-_executeResource(“路由”)
#5/var/www/vhosts/weddingdir/weddingdir/library/Zend/Application/Bootstrap/BootstrapAbstract.php(583):Zend_Application_Bootstrap_Bootstrap_Bootstrap-_Bootstrap(第754行的NUL in/var/www/vhosts/weddingdir/weddingdir/weddingdir/library/Zend/Db/Table/Abstract.php中的NUL)

再次感谢!

它应该按需连接。如果您有一个等效的初始化数据库连接的init方法,您只需要确保在路由之前已引导该方法,如下所示:

protected function _initRoutes()
{
    // this will trigger the _initDb method. 'db' should match
    // the name of that method
    $this->bootstrap('db');

    // routes stuff here
}

protected function _initDb()
{
    // db stuff here
}
编辑:好的,看起来您只需要告诉Zend_Db_Table使用您的Db连接。在代码中,您需要执行以下操作:

Zend_Db_Table_Abstract::setDefaultAdapter($db);
在application.ini中,我认为您可以:

resources.db.isDefaultTableAdapter = true

我猜您目前没有。看看这是否解决了您的问题。有关更完整的示例,请参阅上的DB部分。

My Bootstrap.php中没有_initDb()。我假设它在需要时连接到数据库(它在应用程序的其他任何地方都有)但在Bootstrap内部它没有。我认为这是因为数据库连接信息存储在application.ini文件中,它可能还没有解析该文件或其他什么。application.ini的东西在所有的_init方法之前运行,所以你应该很好。你能举一个例子说明你是如何访问/使用db cl的吗路由方法中的ass?$this->getResource('db');应该按照application.ini数据的设置为您提供db类,因此您可以尝试var_dump()'点击查看您得到了什么。谢谢。我认为您只需要为Zend_Db_表设置默认适配器-我已经编辑了我的答案,以包括一个如何执行此操作的示例。这确实是正确的方法。您必须首先在initRoutes方法中引导'Db',以确保它已初始化。