2010-10-11 52 views
2

我想存储数据库中的Zend框架的自定义路由。我已经创建了路线,但我遇到的问题是,当我添加路线时,它看起来像Zend尚未创建与数据库的连接。强制Zend框架连接到数据库比平常早

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

UPDATE:

什么我从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 /> 

ks再次!

回答

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上的DB部分。

+0

My Bootstrap.php里面没有_initDb()。我会假设它在需要时连接到数据库(它在应用程序中的其他任何地方),但是在Bootstrap内部没有。我在想这是因为数据库连接信息存储在application.ini文件中,并且它可能还没有解析该文件或其他东西。 – 2010-10-11 20:07:29

+0

application.ini的东西在所有的_init方法之前运行,所以你应该没问题。你可以发表你如何访问/使用路径方法中的db类的例子吗? $这 - >的getResource( 'DB');应该根据application.ini数据设置DB类,以便您可以尝试使用var_dump()来查看所获得的结果。 – 2010-10-11 20:09:50

+0

我在第一篇文章中添加了更多信息。 – 2010-10-11 20:28:27