强制Zend Framework比平时更早地连接到数据库

时间:2010-10-11 19:47:41

标签: zend-framework zend-db

我正在尝试在数据库中存储Zend Framework的自定义路由。我有一个将创建路由的进程,但我遇到的问题是,当我添加路由时,看起来Zend还没有创建它与数据库的连接。

有没有人知道这个进程最初发生的位置,或者我如何强制数据库从Bootstrap.php中的init_routes函数连接?

更新:

我在Bootstrap.php中所做的是调用一个模型,该模型将返回供应商的所有Zend_Controller_Router_Route_Static对象。这是我在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);
    }
}

此函数包含在扩展Zend_Db_Table_Abstract

的模型中

我得到的错误如下:

<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 /> 

再次感谢!

1 个答案:

答案 0 :(得分:2)

它应该按需连接。如果你有一个初始化数据库连接的等效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使用你的数据库连接。在代码中你会做:

Zend_Db_Table_Abstract::setDefaultAdapter($db);
在application.ini中的

我认为你可以这样做:

resources.db.isDefaultTableAdapter = true

我猜你现在没有。看看是否能解决您的问题。有关更完整的示例,请参阅http://framework.zend.com/manual/en/zend.application.available-resources.html上的数据库部分。

相关问题